使用Javascript的Twitter OAuth Request_Token,可能是错误的时间
Twitter OAuth Request_Token with Javascript, possibly wrong time?
所以我试图从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
如果有人遇到问题,无法使用这两种解决方案,我可以尝试在网上查找或上传我使用过的库。
相关文章:
- Jquery函数在错误的时间提交
- Javascript设置日期不起作用,显示错误的时间
- Jquery 日期时间选取器错误
- 时刻.js时区值返回错误的时间戳
- 高图表日期时间从错误的日期开始
- 完整的日历错误时间在点击事件谷歌日历
- 使用cordova的android设备上的时间错误,但浏览器中的时间正确
- 检查JavaScript中的日期时间是否相等时出现错误结果
- jQuery setTimeout ajax递归函数在即时消息程序中短时间后抛出错误
- UTC Woes,Flot X轴时间错误
- 谷歌分析/Mixpanel/等.确定时钟错误时的事件时间戳
- new Date返回NaN或从服务器日期()开始的错误时间
- 为什么我从时间戳中得到错误的日期
- 在带有时间戳的日期之间计算时出现 NaN JavaScript 错误
- Chrome 扩展程序弹出窗口关闭在错误的时间触发
- 通过Javascript错误将考试时间设置为30分钟
- jQuery localtime,收到不受支持的日期/时间字符串错误
- j查询返回错误时间的日期格式
- 显示错误时间的完整日历月视图
- 谷歌融合表查询错误-时间比较