如何使用Hapi / bell使用Twitter登录用户?
我正在尝试使用hapi/bell
设置Twitter身份验证流,但是仅遵循简单的示例,它似乎不起作用。这是我的基本代码:
'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] }
我在做什么错?
我正在尝试使用hapi / bell来建立一个Twitter身份验证流,但是仅遵循简单的示例,它似乎不起作用。这是我的基本代码:“使用严格”; const Hapi = require('@ hapi / hapi'); ...
回答如下:
如何使用Hapi / bell使用Twitter登录用户?
我正在尝试使用hapi/bell
设置Twitter身份验证流,但是仅遵循简单的示例,它似乎不起作用。这是我的基本代码:
'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] }
我在做什么错?
我正在尝试使用hapi / bell来建立一个Twitter身份验证流,但是仅遵循简单的示例,它似乎不起作用。这是我的基本代码:“使用严格”; const Hapi = require('@ hapi / hapi'); ...
回答如下: