我尝试使用节点js验证一个evernote用户,一切正常,直到调用回调网址,然后我收到状态码为400的错误
我的代码:
app.get('/api/auth/authenticate', function(req, res){
var client = new Evernote.Client({
consumerKey: evernoteConfig.EVERNOTE_CONSUMER_KEY,
consumerSecret: evernoteConfig.EVERNOTE_CONSUMER_SECRET,
sandbox: true
});
client.getRequestToken(evernoteConfig.callbackURL, function(error,
oauthToken, oauthTokenSecret, results){
if(error) {
req.session.error = JSON.stringify(error);
res.json("Shit:"+req.session.error);
}
else {
// store the tokens in the session
req.session.oauthToken = oauthToken;
req.session.oauthTokenSecret = oauthTokenSecret;
// redirect the user to authorize the token
res.redirect(client.getAuthorizeUrl(oauthToken));
}
});
});
app.get('/api/auth/evernote/callback', function(req, res){
var client = new Evernote.Client({
consumerKey: evernoteConfig.API_CONSUMER_KEY,
consumerSecret: evernoteConfig.API_CONSUMER_SECRET,
sandbox: true
});
client.getAccessToken(
req.session.oauthToken,
req.session.oauthTokenSecret,
req.param('oauth_verifier'),
function(error, oauthAccessToken, oauthAccessTokenSecret, results) {
if(error) {
res.json("Error:"+JSON.stringify(error));
} else {
// store the access token in the session
req.session.oauthAccessToken = oauthAccessToken;
req.session.oauthAccessTtokenSecret = oauthAccessTokenSecret;
req.session.edamShard = results.edam_shard;
req.session.edamUserId = results.edam_userId;
req.session.edamExpires = results.edam_expires;
req.session.edamNoteStoreUrl = results.edam_noteStoreUrl;
req.session.edamWebApiUrlPrefix = results.edam_webApiUrlPrefix;
res.json("Success");
}
});
});
我不知道我做错了什么......也许你们中的某人对evernote js sdk有更多的经验
答案 0 :(得分:0)
查看您的OAuth验证程序并确保其正确无误。我写了一个(相对未经测试的)regrex来正确获取OAuth验证程序。我的(工作)代码非常相似:
function getOauthVerifier(url) {
var regex = new RegExp("[\\?&]oauth_verifier=([^&#]*)"),
results = regex.exec(url);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
...
else if (pathname == "/oauth"){
client.getAccessToken(
global.oauthToken,
global.oauthSecret,
getOauthVerifier(params.search),
function(error, oauthAccessToken, oauthAccessTokenSecret, results) {
if(error) {
console.log("error\n\n\n");
console.log(error);
}
else {
response.writeHead(200, {"Content-Type":"text/html"});
response.write(oauthAccessToken);
response.end();
}
}
);
}
else {
response.writeHead(200, {"Content-Type":"text/html"});
response.write("not a valid URL <a href=\"/\"> GO HOME </a>");
response.end();
}
完整示例:https://discussion.evernote.com/topic/80668-oauth-does-not-work-in-js/#entry341806