如何在NodeJS中正确签署对Twitter的REST API v1.1的HTTP请求? [英] How to correctly sign an HTTP request to Twitter's REST API v1.1 in NodeJS?
问题描述
当我呼叫twitter.request()
时,我只会收到错误32无法验证您的身份"(在我的cloud/twitter.js文件)放在解析云代码中.我已经完成了Twitter告诉我要在创建签名" 中执行的所有操作,然后在
I'm only getting error 32 "Could not authenticate you" when I call twitter.request()
(defined in my cloud/twitter.js file) inside Parse Cloud Code. I've done everything Twitter told me to do in "Creating a Signature" and "Authorizing a request" at dev.twitter.com, but I cannot seem to find how to do so.
其他人是否有使用Parse Cloud Code访问Twitter REST API v1.1的经验?
PS 您可以假设我在Parse.User
中以twitterToken
和twitterTokenSecret
的形式拥有用户的访问令牌"和令牌机密",但是我还提供了ACCESS_TOKEN
和TOKEN_SECRET
常量,因此您不必重新创建我的Parse.User
即可帮助我进行调试.
P.S. You can assume I have the user's "access token" and "token secret" in a Parse.User
as twitterToken
and twitterTokenSecret
, but I have also provided ACCESS_TOKEN
and TOKEN_SECRET
constants so you don't have to recreate my Parse.User
to help me debug this.
推荐答案
事实证明,这很容易解决(谢天谢地).问题在于&
尾随我的签名(facepalm).这是我必须更改的功能.
It turned out to be an easy fix (thankfully). The problem lied in a &
trailing my signature (facepalm). Here's the function I had to change.
function getAuthSignature (user, request, oauth) {
var signingKey, body, signatureBase;
signingKey =
ENCODING_METHOD(CONSUMER_SECRET) + '&' + ENCODING_METHOD(TOKEN_SECRET);
body =
mapObject(oauth, ampersandEncoding)
.concat(mapObject(request.params, ampersandEncoding))
.sort();
// Remove trailing ampersand. (aka the fix)
body.push(body.pop().replace('&', ''));
signatureBase =
request.method.toUpperCase() + '&' +
ENCODING_METHOD(request.url) + '&' +
ENCODING_METHOD(body.join(''));
return crypto.createHmac('sha1', signingKey)
.update(signatureBase)
.digest('base64');
};
这篇关于如何在NodeJS中正确签署对Twitter的REST API v1.1的HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!