Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript API OAuth回调头的格式不正确_Javascript_Api_Twitter_Oauth - Fatal编程技术网

Javascript API OAuth回调头的格式不正确

Javascript API OAuth回调头的格式不正确,javascript,api,twitter,oauth,Javascript,Api,Twitter,Oauth,我正在尝试连接到Twitters API,以便下载我自己的推文。我很难获得Twitter所需的OAuth的正确标题。以下是我到目前为止所拥有的,我是否遗漏了什么或做错了什么 它还要求HMAC-SHA1,它要求两个输入,一个键和数据。我相信根据Twitter的说法,关键是将消费者机密和OAuth令牌机密结合在一起,但是数据输入是什么呢 import axios from 'axios' import hmacsha1 from 'hmacsha1' const tweet = 'http://

我正在尝试连接到Twitters API,以便下载我自己的推文。我很难获得Twitter所需的OAuth的正确标题。以下是我到目前为止所拥有的,我是否遗漏了什么或做错了什么

它还要求HMAC-SHA1,它要求两个输入,一个键和数据。我相信根据Twitter的说法,关键是将消费者机密和OAuth令牌机密结合在一起,但是数据输入是什么呢

import axios from 'axios'
import hmacsha1 from 'hmacsha1'


const tweet = 'http://api.twitter.com/1.1/search/tweets.json?q=SamSchaeferSays';


const consumerKey = "XXX";
let nonce = createNonce();;
const signatureMethod = "HMAC-SHA1";
const timeStamp = Date.now();
const token = "XXXX";
const hiddenToken ="XXXXX";
const sig = `${consumerKey}&${hiddenToken}`;
const signature = btoa(hmacsha1(sig, "ok"));

const DST = `OAuth oauth_consumer_key="${consumerKey}", oauth_nonce="${nonce}", oauth_signature="${signature}", oauth_signature_method="${signatureMethod}", oauth_timestamp="${timeStamp}", oauth_token="${token}", oauth_version="1.0"`;

function createNonce() {
     let randMe = "";
     let possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
         for( var i=0; i < 5; i++ )
             randMe += possible.charAt(Math.floor(Math.random() * possible.length));
     return btoa(randMe);
 }

function getUserTweet() {
    return axios.get(`${tweet}`, { headers: { 'Authorization': `${DST}` } }).then(function(response){
        console.log(response.data)
        console.log(response.status)
    }).catch(function(error) {
        console.log(error)
    });
}
从“axios”导入axios
从“hmacsha1”导入hmacsha1
常量tweet=http://api.twitter.com/1.1/search/tweets.json?q=SamSchaeferSays';
const consumerKey=“XXX”;
设nonce=createNonce();;
const signatureMethod=“HMAC-SHA1”;
const timeStamp=Date.now();
const token=“XXXX”;
const hiddenToken=“XXXXX”;
const sig=`${consumerKey}&${hiddenToken}`;
常量签名=btoa(hmacsha1(sig,“ok”);
const DST=`OAuth OAuth_consumer_key=“${consumerKey}”、OAuth_nonce=“${nonce}”、OAuth_signature=“${signature}”、OAuth_signature_方法=“${signatureMethod}”、OAuth_timestamp=“${timestamp}”、OAuth_token=“${token}”、OAuth_version=“1.0”`;
函数createNonce(){
让我=”;
允许=“ABCDEFGHIJKLMNOPQRSTUVXYZABCDFGHIJKLMNOPQRSTUVXYZ0123456789”;
对于(变量i=0;i<5;i++)
randMe+=可能的.charAt(Math.floor(Math.random()*可能的.length));
返回btoa(randMe);
}
函数getUserTweet(){
返回axios.get(`${tweet}`,{headers:{'Authorization':`${DST}}}){
console.log(response.data)
console.log(response.status)
}).catch(函数(错误){
console.log(错误)
});
}

数据基本上是您的请求(方法和参数),编码方式如上所述。
最终会有类似的结果

urlEncode(headers.keys().sort().map(key => `${key}=${headers[key]}`).join("&"))

是的,我已经阅读了API网站,这就是我如何做到这一点的。我认为问题在于我的签名-我认为你也必须为签名重新创建整个DST。这有点让人困惑是的,所有的标题都必须按字母顺序放在那里。类似于
headers.keys().sort().map(key=>key+“=”+headers[key]).join(&)
的东西是错误的,因为它有逗号和空格,应该是一个长字符串,带有
&
(然后是
urlEncode
),我认为DST字符串是正确的。通过遵循twitterapi规则,将字符串“OAuth”(包括末尾的空格)附加到DST。对于上面列出的7个参数的每个键/值对:对键进行百分比编码并将其附加到DST。将等号字符“=”附加到DST。在DST后面加上双引号'''。百分比对值进行编码并将其附加到DST。在DST后面加上双引号'''。如果仍有键/值对,请在DST后面附加逗号“,”和空格“”。我的签名不正确,因为您必须基本上将DST重新生成为签名,然后将其与原始DST一起包含