我正在尝试使用hapi/bell来设置一个twitter auth流,但是简单地按照这个简单的例子,它似乎不起作用。下面是我的基本代码:
'use strict';
const Hapi = require('@hapi/hapi');
const Bell = require('@hapi/bell');
const init = async () => {
const server = Hapi.server({port: 3333, host: 'localhost'});
// Register bell with the server
await server.register(Bell);
server.auth.strategy('twitter', 'bell', {
provider: 'twitter',
password: 'test_test',
clientId: 'my_consumer_api_key',
clientSecret: 'my_consumer_api_key_secret',
isSecure: false
});
server.route({
method: 'GET', // Must handle both GET and POST
path: '/bell/door', // The callback endpoint registered with the provider
options: {
auth: 'twitter',
handler: function(request, h) {
if (!request.auth.isAuthenticated) {
return `Authentication failed due to: ${request.auth.error.message}`;
}
return h.redirect('/home');
}
}
});
server.events.on('request', (request, event, tags) => {
if (event.channel === 'error') {
console.dir(event.error.data, 4);
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
};
// process.on('unhandledRejection', err => {
// console.log(err);
// process.exit(1);
// });
init();下面是我的应用程序的配置:

但是当我用我的浏览器访问http://localhost:3333/bell/door时,我得到了这个错误:
{ Error: Failed obtaining twitter temporary credentials
at exports.Client.internals.Client.internals.Client._request (/Users/ben/pro/tmp/authy/node_modules/@hapi/bell/lib/oauth.js:453:20)
at process._tickCallback (internal/process/next_tick.js:68:7)
data:
{ Error: Response Error: 403 Forbidden
at internals.Client._shortcut (/Users/ben/pro/tmp/authy/node_modules/@hapi/wreck/lib/index.js:635:11)
at process._tickCallback (internal/process/next_tick.js:68:7)
data:
{ isResponseError: true,
headers: [Object],
res: [IncomingMessage],
payload:
<Buffer 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 55 54 46 2d 38 22 3f 3e 3c 65 72 72 6f 72 73 3e 3c 65 72 72 ... > },
isBoom: true,
isServer: false,
output: { statusCode: 403, payload: [Object], headers: {} },
reformat: [Function],
typeof: [Function: Error] },
isBoom: true,
isServer: true,
output:
{ statusCode: 500,
payload:
{ statusCode: 500,
error: 'Internal Server Error',
message: 'An internal server error occurred' },
headers: {} },
reformat: [Function],
typeof: [Function: internal] }我做错了什么?
发布于 2019-11-06 11:36:01
我来晚了一点,但你需要更改你的消费者和客户端密钥。您当前正在设置'my_consumer_api_key‘和'my_consumer_api_key_secret’。将它们替换为您可以在twitter应用程序中找到的值。
此外,您的回调方法是/login,而GET路由中的路径是/bell/door。请更改其中一个以使其匹配。希望它能帮上忙!
https://stackoverflow.com/questions/57762992
复制相似问题