推特 API Request_Token Angular

Twitter API Request_Token in Angular

所以,我在为我的 angular 应用创建 Twitter 登录方法时遇到了问题。问题是我什至无法完成第一步。我需要向 request_token API 发出 post 请求(我正在执行此步骤https://dev.twitter.com/web/sign-in/implementing) 并且我仍然收到“错误的身份验证数据”错误(只是想让你知道:我一直在弄乱我的代码来完成这项工作并且我得到的最好的是“无法验证你”错误)。

所以在我展示我的代码之前,让我确保我理解正确。由于我正在 请求令牌 ,因此我不会在我的 access_token 中传递任何内容代码。我在创建签名时并没有在任何地方使用令牌,而是在授权请求中传递 oauth_callback 字符串。

所以,这是我将 POST 转换为 request_token 的代码。

callTwLogin(): void{
    const url = 'https://api.twitter.com/oauth/request_token';
    const callback = encodeURIComponent('http://127.0.0.1/');

    this.Oauth_timestamp = this.createTimestamp(); // We create a timestamp in seconds.
    this.Oauth_nonce = this.randomString(32); // We create a 32-long random string.

    this.http.post(url, {headers: new HttpHeaders().set('Content-Type','application/x-www-form-urlencoded')
                    .set('Authorization','OAuth oauth_consumer_key="'+this.Oauth_consumerkey+'", oauth_nonce="'+this.Oauth_nonce+'"oauth_signature="'+encodeURIComponent(this.createSignature())+'", oauth_signature_method="HMAC-SHA1", oauth_timestamp="'
                    +this.Oauth_timestamp+'", oauth_version="1.0"')
                    })
      .subscribe(rsp => console.log("Twitter: " +rsp)); // Should it have Access Token if it is a request token?
   }

createTimestamp() 和 randomString() 函数非常简单,因此我不会展示它们。但是,createSignature() 函数非常重要,所以这里是:

createSignature():string{
    let rawURL: string = "POST&" + encodeURIComponent("https://api.twitter.com/oauth/request_token") + "&";
    let parameterString: string = "include_entities=true" + 
                                    "&oauth_consumer_key=" + this.Oauth_consumerkey + 
                                    "&oauth_nonce=" + this.Oauth_nonce + 
                                    "&oauth_signature_method=HMAC-SHA1"+ 
                                    "&oauth_timestamp=" + this.Oauth_timestamp + 

                                    "&oauth_version=1.0";
    let signingString = rawURL + encodeURIComponent(parameterString);
    let signingKey = encodeURIComponent(this.ConsumerSecret) + "&"; // No TokenSecret because its Request_Token.
    let signatur: string = this.CryptoJS.HmacSHA1(signingString, signingKey).toString(this.CryptoJS.enc.Base64);
    console.log("Signatur: " + signatur);
    return signatur;
   } 

如您所见,我删除了签名基础字符串和签名密钥中的令牌。我究竟做错了什么?我已经为此苦苦挣扎了数周,但无法完成。

请帮助我。 谢谢!

===================================更新 1 ======== ======================

所以我做了 Jon Susiak 让我做的事。我添加了我之前忘记添加的逗号,我将 oauth_callback 添加到我的参数字符串和我的授权请求中,我删除了 include_entities

这是我的新 createSignature 方法:

let callback = "http://127.0.0.1/"
    let rawURL: string = "POST&" + encodeURIComponent("https://api.twitter.com/oauth/request_token") + "&";
    let parameterString: string =   "oauth_callback=" +callback+
                                    "&oauth_consumer_key=" + this.Oauth_consumerkey + 
                                    "&oauth_nonce=" + this.Oauth_nonce + 
                                    "&oauth_signature_method=HMAC-SHA1"+ 
                                    "&oauth_timestamp=" + this.Oauth_timestamp +        
                                    "&oauth_version=1.0";
    let signingString = rawURL + encodeURIComponent(parameterString);
    let signingKey = encodeURIComponent(this.ConsumerSecret) + "&"; // No TokenSecret because its Request_Token.
    let signatur: string = this.CryptoJS.HmacSHA1(signingString, signingKey).toString(this.CryptoJS.enc.Base64);
    console.log("Signatur: " + signatur);
    return signatur;

还有我的新POST代码

const url = 'https://api.twitter.com/oauth/request_token';
    const callback = encodeURIComponent('http://127.0.0.1/');
    const body = {
      oauth_callback: callback
    };
    this.Oauth_timestamp = this.createTimestamp(); // We create a timestamp in seconds.
    this.Oauth_nonce = this.randomString(32); // We create a 32-long random string.
    this.http.post(url, {headers: new HttpHeaders()
                    .set('Authorization','OAuth oauth_callback="' + callback
                    + '", oauth_consumer_key="' + this.Oauth_consumerkey
                    + '", oauth_nonce="' + this.Oauth_nonce
                    + '", oauth_signature="' + encodeURIComponent(this.createSignature())
                    + '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' + this.Oauth_timestamp
                    + '", oauth_version="1.0"')
                    })
      .subscribe(rsp => console.log("Twitter: " +rsp)); 

它仍然不起作用,如果我不将我的回调作为 POST 参数发送,它会抛出 'Bad Authentication Data (Error 400)'。如果我确实将它作为参数发送,它会抛出 'Could not authenticate you (Error 401)'

请帮忙!

首先include_entities不是获取请求令牌时使用的参数。获得访问令牌后,可以稍后使用此参数。

其次,您没有在参数字符串或授权 header 中包含 oauth_callback 参数。

第三,您在授权 header 中的 oauth_nonce 后遗漏了一个逗号。请注意,如有必要,参数值应进行百分比编码。

因此您的参数字符串应如下所示:

let parameterString: string = "oauth_callback" + callback
"&oauth_consumer_key=" + this.Oauth_consumerkey + 
"&oauth_nonce=" + this.Oauth_nonce + 
"&oauth_signature_method=HMAC-SHA1"+ 
"&oauth_timestamp=" + this.Oauth_timestamp + 
"&oauth_version=1.0";

您的授权 header 应该是:

'OAuth oauth_callback="' + callback
+ '", oauth_consumer_key="' + this.Oauth_consumerkey
+ '", oauth_nonce="' + this.Oauth_nonce
+ '", oauth_signature="' + encodeURIComponent(this.createSignature())
+ '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' + this.Oauth_timestamp
+ '", oauth_version="1.0"'

有更新:

你从不使用 body。是第二个参数,最后的选项是第三个。