我正在尝试从他们的 oauth api 获取 Twitter 访问令牌。我使用的插件是这个https://code.google.com/p/oauth/source/browse/#svn%2Fcode%2Fjavascript。到目前为止,我只收到“401 无法验证签名和令牌”。
奇怪的是,即使我设置了 type:'POST',我的 ajax 调用也变成了 'GET' 请求。似乎 jquery 正在将类型从 POST 更改为 GET。我不知道为什么会这样。我在我的 Mac 上运行它。感谢您的帮助/提示/建议/建议。谢谢!
$(函数(){
函数 myCallback(resp) {
控制台.log(resp);
}
var TwitterAPI;
TwitterAPI = (函数() {
var consumer_key = null;
var consumer_secret = null;
函数 TwitterAPI(cons_key, cons_secret) {
this.consumer_key = cons_key;
this.consumer_secret = cons_secret;
}
TwitterAPI.prototype._url = 函数(数据){
if (typeof data == 'array') {
return array_map([ // TODO
这个,'_url'],数据);
} else if ((/boolean|number|string/).test(typeof data)) {
return encodeURIComponent(data).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\ )/g, '%29').replace(/\*/g, '%2A');
} 别的 {
返回 '';
}
}
TwitterAPI.prototype.myCallback = function(resp) {
控制台.log(resp);
}
TwitterAPI.prototype.getRequestToken = function() {
var 访问器 = {
consumerSecret: this.consumer_secret, //this.consumer.consumerSecret,
令牌秘密:''
};
变量消息 = {
方法:“POST”,
行动:“https://api.twitter.com/oauth/request_token”,
参数: {
oauth_signature_method:“HMAC-SHA1”,
oauth_consumer_key: this.consumer_key, //this.consumer.consumerKey
oauth_callback: this._url("http://127.0.0.1/foobar/libs/oauth/wtf.html"),
}
};
OAuth.setTimestampAndNonce(消息);
OAuth.SignatureMethod.sign(消息,访问者);
var target = OAuth.addToURL(message.action, message.parameters);
message.parameters.oauth_signature = this._url(message.parameters.oauth_signature);
console.log(message.parameters);
$.ajax("https://api.twitter.com/oauth/request_token",
{ 网址:“https://api.twitter.com/oauth/request_token”,
类型:'POST',
数据类型:'jsonp',
jsonp: '回调',
jsonpCallback:“我的回调”,
数据:消息.参数,
成功:函数(数据,textResp,xhr){
控制台.log(数据);
},
错误:函数(xhr,文本,错误){
控制台.log(文本);
}
});
};
返回推特API;
})();
api = 新的 TwitterAPI(密钥,秘密);
$('button#request').on('click', function(e) {
e.stopPropagation();
api.getRequestToken();
});