I'm facing some challenges while trying to implement a Twitter Sign-In method for my angular app. The issue seems to be with the initial step itself. I am attempting to make a post request to the request_token API by following the steps outlined at this link. However, I keep encountering the 'Bad Authentication Data' error (I've made several attempts and the best outcome so far has been the 'Could not authenticate you' error).
Before diving into my code, I want to ensure that my understanding is correct. As I am requesting a token, I should not include any access_token in my code. Additionally, I do not utilize the token when generating my signature, but instead include the oauth_callback string in the Authorization request.
This is the code snippet responsible for making the POST request to 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(); // Timestamp in seconds
this.Oauth_nonce = this.randomString(32); // Random 32-character 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));
}
I have omitted the details of createTimestamp() and randomString() functions as they are straightforward. However, the createSignature() function plays a crucial role:
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) + "&";
let signatur: string = this.CryptoJS.HmacSHA1(signingString, signingKey).toString(this.CryptoJS.enc.Base64);
console.log("Signatur: " + signatur);
return signatur;
}
You may notice that I have removed the token from both the signature base string and the signinkey. I have been struggling with this for weeks without success. Any help would be greatly appreciated!
Thank you!
===================================UPDATE 1 ==============================
In response to Jon Susiak's suggestion, I made necessary adjustments such as adding the missing comma, including oauth_callback in both the parameterString and Authorization Request, and removing the include_entities. Here is the modified createSignature Method:
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) + "&";
let signatur: string = this.CryptoJS.HmacSHA1(signingString, signingKey).toString(this.CryptoJS.enc.Base64);
console.log("Signatur: " + signatur);
return signatur;
And here is the updated POST Code:
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();
this.Oauth_nonce = this.randomString(32);
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));
Despite these modifications, the issue persists. When I omit the callback parameter in the POST request, it results in a 'Bad Authentication Data (Error 400)' message. Conversely, including the callback parameter triggers a 'Could not authenticate you (Error 401)'.
Your assistance is highly appreciated!