0

尝试获取请求令牌时,我收到了正文“无法验证 oauth 签名和令牌”的响应。

这是我用来设置所有请求参数的代码。我注意到一些有趣的地方,我认为这些地方可能是一堆星号的问题。

var appId = "myId"
  , appSecret = "mySecret"
  , redirectUrl = "http://localhost:8077/twitterLogin";

var d = new Date();
  , time = Math.floor(d.getTime() / 1000); //seconds since epoch

var oauth_nonce = Math.random() * 1000000; //************could be the issue, maybe?
  , oauth_callback = encodeURIComponent('http://localhost:8077/twitterLogin');

//****************more likely the issue
var paramString = encodeURIComponent('oauth_consumer_key=**MY_APP_ID**&oauth_callback='+ oauth_callback 
                                      + '&oauth_nonce=' + oauth_nonce 
                                      + '&oauth_signature_method=HMAC-SHA1&oauth_timestamp=' + time 
                                      + '&=oauth_version=1.0');
var baseString = "POST&" + encodeURIComponent("https://api.twitter.com/oauth/request_token") + '&' + paramString; 

var signingKey = encodeURIComponent(appSecret) + '&' + encodeURIComponent(appSecret);
  , signature = crypto.createHmac('sha1', signingKey).update(baseString).digest('hex');

这是请求本身的代码:

var requestBody = "oauth_callback="+ oauth_callback 
                + "&appId=" + appId
                + "&oauth_nonce=" + oauth_nonce 
                + "&oauth_signature=" + signature 
                + "&oauth_signature_method=HMAC-SHA1"
                + "&oauth_timestamp=" + time
                + "&oauth_version=1.0";
//*******also could be the issue. maybe missing headers or something?
request.post({url: 'https://api.twitter.com/oauth/request_token', body: requestBody}); 

我只是想知道签名或令牌缺少什么..

4

1 回答 1

1
  • Firstly your parameters have to be sorted lexigraphically (alphabetically) before they are encoded, you need to switch the positions of oauth_callback and oauth_consumer_key.

  • Secondly, for an unauthorized request token, you calculate the signing key using your consumer secret appended with the '&' character. You have appended the secret a second time after the ampersand.

  • Thirdly in your request body you should use oauth_consumer instead of appId as the name of your parameter.

Try those fixes and see if it works.

于 2013-06-07T10:00:05.877 回答