使用Javascript的Twitter OAuth Request_Token,可能是错误的时间

Twitter OAuth Request_Token with Javascript, possibly wrong time?

本文关键字:错误 时间 Twitter Javascript OAuth Request 使用 Token      更新时间:2023-09-26

所以我试图从twitter获取令牌,但失败了。我得到以下错误:"无法验证oauth签名和令牌"。我读过,可能是由于您的系统时钟错误。在javascript中,我用以下代码测试了我的约会

var minutes=1000*60;
var hours=minutes*60;
var days=hours*24;
var years=days*365;
var d=new Date();
var t=d.getTime();
var y=t/years;
console.log((y+1970) + " year and " + (t%years)/days)

这给了我2012年和17天的时间。。1972197619801984198819921996200020042008__=10闰日。今天是8号,所以去掉闰日,我的系统时钟似乎在7号?或者我在这里犯了错误?如果这是问题所在,我该如何修复并更正我的时钟?

在cmd中,当我执行日期cmd时,它会给我今天的日期,即8号。

这是我的帖子请求和代码,以防问题出在代码而不是时钟上。

我的帖子请求是:

POST http://api.twitter.com/oauth/request_token?oauth_callback=127.0.0.1&oauth_consumer_key=FFZJrBaPLsiwTDg5159tTQ&oauth_nonce=tWHEEIW8vLS6tMggo3IXe6e449qv1GpE8LunKRsbRF&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1326039495&oauth_version=1.0&oauth_signature=d%2BQqgTzJCjYIp9vKwm%2BCWzVLPvA 

获得401(未授权)

这是我的javascript代码。

var url = "http://api.twitter.com/oauth/request_token";
var params={
oauth_callback : "127.0.0.1"
,oauth_consumer_key : "FFZJrBaPLsiwTDg5159tTQ"
,oauth_nonce : OAuth.nonce(42)
,oauth_signature_method : "HMAC-SHA1"
,oauth_timestamp : OAuth.timestamp()
,oauth_version: "1.0"}
//temp is to be the signature base string
var temp = toSignParams("POST",url,params); 
console.log(temp);
//This logs the signature base string as  "POST&http%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&oauth_callback%3D127.0.0.1%26oauth_consumer_key%3DFFZJrBaPLsiwTDg5159tTQ%26oauth_nonce%3D5gQVIa3WmwD6ARGGQTITl1Ozgxe2t8em5HC7g8wvMi%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1326038871%26oauth_version%3D1.0"
//which is correct I think.
//When I use this with the base signature from twitters oauth example page I get the result they got.
//it hashes the twitter signing key with base signature.
params.oauth_signature = b64_hmac_sha1("MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh98&",temp);

var req = new XMLHttpRequest();
req.open("POST",toURIParams(url,params),true);  
req.send();
console.log(params)
req.onreadystatechange=function(){
if (req.readyState==4) 
    {
    console.log(req.responseText); //this is saying "Failed to validate oauth signature and token"
    }
}
//function to convert to Signature paramaters, as indicated on twitter page.
function toSignParams(method,base,params){
    tail=[];
    for (var p in params) {
        if (params.hasOwnProperty(p)) {
            tail.push(p + "%3D" + encodeURIComponent(params[p]));
        }
    }
    return method + "&" + encodeURIComponent(base) + "&" + tail.join("%26")
}
//function to convert to uri encoded parameters.
function toURIParams(base, params) {
  tail = [];
  for (var p in params) {
    if (params.hasOwnProperty(p)) {
      tail.push(p + "=" + encodeURIComponent(params[p]));
    }
  }
  return base + "?" + tail.join("&")
}

有什么想法吗?

我使用了一个名为"jsOAuth-1.3.3"的库,该库在twitter上与JavaScript兼容的库列表中找到,但现在已经不存在了。

他们现在列出的两个javascript选项是:

  • AivisSilins的用户流——一个简单的Node.js用户流客户端以及
  • @mynetx的codebird js——JScript中的一个Twitter库

在上找到https://dev.twitter.com/docs/twitter-libraries

如果有人遇到问题,无法使用这两种解决方案,我可以尝试在网上查找或上传我使用过的库。