我正试图从他们的oauth api获取Twitter访问令牌。我使用的插件是https://code.google.com/p/oauth/source/browse/#svn%2Fcode%2Fjavascript。到目前为止,我只得到“401无法验证签名和令牌”。
奇怪的是,即使我设置了类型:'POST',我的ajax调用也会成为'GET'请求。似乎jquery正在将类型从POST更改为GET。我不知道为什么会这样。我正在Mac上运行它。我感谢您的帮助/提示/建议/建议。谢谢!
$(function() { function myCallback(resp) { console.log(resp); } var TwitterAPI; TwitterAPI = (function() { var consumer_key = null; var consumer_secret = null; function TwitterAPI(cons_key, cons_secret) { this.consumer_key = cons_key; this.consumer_secret = cons_secret; } TwitterAPI.prototype._url = function (data) { if (typeof data == 'array') { return array_map([ // TODO this, '_url'], data); } 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'); } else { return ''; } } TwitterAPI.prototype.myCallback = function(resp) { console.log(resp); } TwitterAPI.prototype.getRequestToken = function() { var accessor = { consumerSecret: this.consumer_secret, //this.consumer.consumerSecret, tokenSecret: '' }; var message = { method: "POST", action: "https://api.twitter.com/oauth/request_token", parameters: { 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(message); OAuth.SignatureMethod.sign(message, accessor); 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", { url: "https://api.twitter.com/oauth/request_token", type: 'POST', dataType: 'jsonp', jsonp: 'callback', jsonpCallback: "myCallback", data: message.parameters, success: function(data, textResp, xhr) { console.log(data); }, error: function(xhr, text, err) { console.log(text); } }); }; return TwitterAPI; })(); api = new TwitterAPI(key, secret); $('button#request').on('click', function(e) { e.stopPropagation(); api.getRequestToken(); });