Hello.
I try to use streaming api for my browser extension (currently do test on Chrome).
Because it is browser extension, I need to implement purely in Javascript.
access token & access token secret are correctly obtained (I find exactly same value listed in OAuth Signature Generator).
And, when I try to access https://userstream.twitter.com/1.1/user.json with GET, I can’t receive any response (my XMLHttpRequest doesn’t work. status == 0 and, readyState == 1, even I call xhr.send() ).
On the other hand, when I try to access https://stream.twitter.com/1.1/statuses/sample.json with GET (all settings except for endpoint URL are unchange), I correctly receive tweets from Twitter.
Note: I connect to https://userstream.twitter.com/1.1/user.json with cURL, it works.
I share you my code snippet below:
var CryptoJS = require("../../node_modules/crypto-js");
var apiKey = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
var apiSecret = "YYYYYYYYYYYYYYYYYYYYYYYYY";
var accessTokenKey = "ZZZZZZZZZZZZZZZZZZZZZZZZZZ";
var accessTokenSecret = "WWWWWWWWWWWWWWWWWWW";
function makeStream(){
var requestURL = "https://userstream.twitter.com/1.1/user.json";
var requestMethod = "GET";
var signatureKey = encodeURIComponent(apiSecret)+"&"+encodeURIComponent(accessTokenSecret);
// generate parameters
var msec = Date.now();
var params = {
"oauth_consumer_key" : apiKey,
"oauth_nonce" : msec,
"oauth_signature_method" : "HMAC-SHA1",
"oauth_timestamp" : Math.floor(msec / 1000),
"oauth_token" : accessTokenKey,
"oauth_version" : "1.0"
};
// encode each parameter
var sortedKeys = []
for(var key in params){
params[key] = encodeURIComponent(params[key]);
sortedKeys.push(key)
}
sortedKeys.sort();
var requestParams = "";
for(var key in sortedKeys){
requestParams += sortedKeys[key];
requestParams += "=";
requestParams += params[sortedKeys[key]];
requestParams += "&";
}
requestParams = String(requestParams).substring(0, requestParams.length-1)
for(var key in sortedKeys){
requestParams += sortedKeys[key];
requestParams += "=";
requestParams += params[sortedKeys[key]];
requestParams += "&";
}
requestParams = String(requestParams).substring(0, requestParams.length-1)
var encodedRequestMethod = encodeURIComponent(requestMethod);
var encodedRequestURL = encodeURIComponent(requestURL);
var signatureData = encodedRequestMethod+"&"+encodedRequestURL+"&"+encodeURIComponent(requestParams);
var hash = CryptoJS.HmacSHA1(signatureData,signatureKey);
var signature = CryptoJS.enc.Base64.stringify(hash);
params["oauth_signature"] = encodeURIComponent(signature);
var headerParams = ""
for(var key in params){
headerParams += key
headerParams += "="
headerParams += '"'
headerParams += params[key]
headerParams += '"'
headerParams += ", "
}
headerParams = String(headerParams).substring(0, headerParams.length-2);
xhr = new XMLHttpRequest();
xhr.open(requestMethod, requestURL, true);
xhr.setRequestHeader("Authorization", "OAuth "+headerParams);
xhr.onreadystatechange = function () {
// this is just for debug. And, this callback function doesn't fire.
console.log(xhr.status);
console.log(xhr.readyState);
console.log(xhr.responseText);
}
xhr.send(null);
console.log("status: " + xhr.status); // status == 0
console.log("readystate: " + xhr.readyState); // readyState == 1. even I call send() function