我正在使用 node.js(10.x) 和 express(3.x) 创建 Web 应用程序。
我选择了node-oauth作为 twitter oauth 模块。
但是在请求Token时,会返回以下错误。
{ statusCode: 401, data: '<?xml version="1.0" encoding="UTF-8"?>\n\n <request>/oauth/request_token</request>\n <error>考虑给定的 URL恶意软件</error>\n</hash>\n' }
身份验证已成功多次,但突然发送此错误,之后我从未成功过。
怎么了?多次请求是什么原因?
代码如下
var oauth = require('oauth');
var consumer = new oauth.OAuth(
'https://api.twitter.com/oauth/request_token'
, 'https://api.twitter.com/oauth/access_token'
, 'My consumer key'
, 'My consumer secret'
, '1.0A'
, 'http://localhost:3000/sessions/access' //I'm testing in local
, 'HMAC-SHA1'
);
/** In the actual code, Some express settings exist like followings
* var express = require('express');
* var app = express();
* app.set(foo, bar);
* app.use(baz);
* and so on.
*/
app.get('/sessions/connect', function(req, res, next) {
consumer().getOAuthRequestToken(function(err, oauthToken, oauthTokenSecret, results) {
if (err) { //This err says the above error.
next(err);
} else {
req.session.oauthRequestToken = oauthToken;
req.session.oauthRequestTokenSecret = oauthTokenSecret;
res.redirect('https://api.twitter.com/oauth/authorize?oauth_token=' + oauthToken);
}
});
});
app.get('/sessions/access', function(req, res, next) {
consumer().getOAuthAccessToken(req.session.oauthRequestToken, req.session.oauthRequestTokenSecret, req.query.oauth_verifier, function(err, oauthAccessToken, oauthAccessTokenSecret, results) {
if (err) {
next(err);
} else {
req.session.oauthAccessToken = oauthAccessToken;
req.session.oauthAccessTokenSecret = oauthAccessTokenSecret;
req.session.user_id = results.user_id;
req.session.isAuthorized = true;
res.redirect('/');
}
});
});
谢谢