sreejithbnaick / twitter-oauth-1.1-signature-generator-js Goto Github PK
View Code? Open in Web Editor NEWTwitter OAuth Signature Generator for Parse Cloud Code
License: Apache License 2.0
Twitter OAuth Signature Generator for Parse Cloud Code
License: Apache License 2.0
Hi, thanks for the Parse implementation. Could you possibly post an example on how to get a User's tweets with the user_id param? Using the user_timeline url
I tried adding the "user_id" to the params object and changing the url, but I get an auth error (32).
I can get my own timeline fine, with out adding the user_id to the param, but not another User's.
Thanks!
I believe that the Authorization link in the README.md is confusing. I'd change it to:
"Authorization": 'OAuth oauth_consumer_key="' + oauth_consumer_key + '", oauth_nonce=' + nonce + ', oauth_signature=' + encodedSig + ', oauth_signature_method="HMAC-SHA1", oauth_timestamp=' + timestamp + ',oauth_token="' + oauth_token + '", oauth_version="1.0"'
This allows copy and paste of all code, with the necessity to only change the:
I'm trying to use the new https://upload.twitter.com/1.1/media/upload.json endpoint and regardless of the header variations I put in I keep getting "Request failed with response 401 , {"errors":[{"message":"Could not authenticate you","code":32}]}".
Can't produce and example of using this endpoint, I've feel like I've searched all Google has to offer and I can't find a working example. Here is what my code looks like:
var urlLink = 'https://upload.twitter.com/1.1/media/upload.json';
var consumerSecret = "XXXXXX";
var tokenSecret = "XXXXXX";
var oauth_consumer_key = "XXXXXX";
var oauth_token = "XXXXXX";
var nonce = oauth.nonce(32);
var ts = Math.floor(new Date().getTime() / 1000);
var timestamp = ts.toString();
var accessor = {
"consumerSecret": consumerSecret,
"tokenSecret": tokenSecret
};
var params = {
"oauth_version": "1.0",
"oauth_consumer_key": oauth_consumer_key,
"oauth_token": oauth_token,
"oauth_timestamp": timestamp,
"oauth_nonce": nonce,
"oauth_signature_method": "HMAC-SHA1",
"media_data": "data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAA"
};
var message = {
"method": "POST",
"action": urlLink,
"parameters": params
};
//lets create signature
oauth.SignatureMethod.sign(message, accessor);
var normPar = oauth.SignatureMethod.normalizeParameters(message.parameters);
var baseString = oauth.SignatureMethod.getBaseString(message);
var sig = oauth.getParameter(message.parameters, "oauth_signature") + "=";
var encodedSig = oauth.percentEncode(sig);
Parse.Cloud.httpRequest({
method: 'POST',
url: urlLink,
headers: {
"Authorization": 'OAuth oauth_consumer_key="' + oauth_consumer_key + '", oauth_nonce="' + nonce + '", oauth_signature="' + encodedSig + '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' + timestamp + '", oauth_token="' + oauth_token + '", oauth_version="1.0"',
'Content-Transfer-Encoding': 'base64',
'Content-Type': 'application/x-www-form-urlencoded'
},
success: function(httpResponse) {
res.send(httpResponse.text);
},
error: function(httpResponse) {
res.send('Request failed with response ' + httpResponse.status + ' , ' + httpResponse.text);
}
});
"Timeline" cloud function in the samples work without problems. I can successfully get the Twitter timeline of my user. However the status updating function gets 401 from Twitter API.
Also in the sample code you have a variable declaration "status" on this line:
var status = oauth.percentEncode(postSummary);
However, you don't use this variable anywhere or am I missing something obvious?
Hi,
I'm trying to do a simple search call to twitter api but I get:
{"errors":[{"message":"Could not authenticate you","code":32}]}
This is my code:
var urlLink = 'https://api.twitter.com/1.1/search/tweets.json';
var searchQuery="#mtv vine.co filter:links since:2011-11-11";
var q = oauth.percentEncode(searchQuery);
var consumerSecret = "<myConsumerSecret>";
var tokenSecret = "<myTokenSecret>";
var oauth_consumer_key = "<myConsumerKey>";
var oauth_token = "<my_token>";
var nonce = oauth.nonce(32);
var ts = Math.floor(new Date().getTime() / 1000);
var timestamp = ts.toString();
var accessor = {"consumerSecret" : consumerSecret, "tokenSecret":tokenSecret};
var message = {"method" : "GET", "action": urlLink, "parameters":oauth.decodeForm("q="+q)};
message.parameters.push(["oauth_version","1.0"]);
message.parameters.push(["oauth_consumer_key", oauth_consumer_key]);
message.parameters.push(["oauth_token", oauth_token]);
message.parameters.push(["oauth_timestamp",timestamp]);
message.parameters.push(["oauth_nonce", nonce]);
message.parameters.push(["oauth_signature_method", "HMAC-SHA1"]);
//lets create signature
oauth.SignatureMethod.sign(message, accessor);
var normPar = oauth.SignatureMethod.normalizeParameters(message.parameters);
var baseString = oauth.SignatureMethod.getBaseString(message);
var sig = oauth.getParameter(message.parameters, "oauth_signature")+"=";
var encodedSig=oauth.percentEncode(sig); //finally you got oauth signature
Parse.Cloud.httpRequest({
method: 'GET',
url: urlLink,
headers: {
"Authorization": 'OAuth oauth_consumer_key="'+oauth_consumer_key+'", oauth_nonce='+nonce+', oauth_signature='+encodedSig+', oauth_signature_method="HMAC-SHA1", oauth_timestamp='+timestamp+',oauth_token="'+oauth_token+'", oauth_version="1.0"'
},
success: function(httpResponse) {
status.success(httpResponse.text);
},
error: function(httpResponse) {
status.error('Request failed with response' + httpResponse.text);
}
});
When following your sample code, posting a status of something like "It's hot outside" will cause the generic "Could not authenticate" error message. If you remove or replace the single quote with an alphanumeric character, it works fine. I'm sure this has something to do with the URL encoding but I haven't been able to find the issue yet. Any suggestions? Thanks for your help on this.
Can you add support for multipart upload with media post to Twitter?
I'm trying to return a user timeline for a specific screen_name and all I'm getting back is 401 errors. The Cloud Code I'm using in my main.js:
Parse.Cloud.define("twitterTimeline", function(request, response) {
var screen_name = "myscreenname";
var urlLink = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
var consumerSecret = "myconsumersecret";
var tokenSecret = "mytokensecret";
var oauth_consumer_key = "myoauthkey";
var oauth_token = "myoauthtoken";
var nonce = oauth.nonce(32);
var ts = Math.floor(new Date().getTime() / 1000);
var timestamp = ts.toString();
var accessor = {
"consumerSecret": consumerSecret,
"tokenSecret": tokenSecret
};
var params = {
"oauth_version": "1.0",
"oauth_consumer_key": oauth_consumer_key,
"oauth_token": oauth_token,
"oauth_timestamp": timestamp,
"oauth_nonce": nonce,
"oauth_signature_method": "HMAC-SHA1",
"screen_name": screen_name,
"count": 5,
"include_rts": 1
};
var message = {
"method": "GET",
"action": urlLink,
"parameters": params
};
//lets create signature
oauth.SignatureMethod.sign(message, accessor);
var normPar = oauth.SignatureMethod.normalizeParameters(message.parameters);
console.log("Normalized Parameters: " + normPar);
var baseString = oauth.SignatureMethod.getBaseString(message);
console.log("BaseString: " + baseString);
var sig = oauth.getParameter(message.parameters, "oauth_signature") + "=";
console.log("Non-Encode Signature: " + sig);
var encodedSig = oauth.percentEncode(sig); //finally you got oauth signature
console.log("Encoded Signature: " + encodedSig);
Parse.Cloud.httpRequest({
method: 'GET',
url: urlLink,
headers: {
"Authorization": 'OAuth oauth_consumer_key="'+oauth_consumer_key+'", oauth_nonce=' + nonce + ', oauth_signature=' + encodedSig + ', oauth_signature_method="HMAC-SHA1", oauth_timestamp=' + timestamp + ',oauth_token="'+oauth_token+'", oauth_version="1.0"'
},
body: {
},
success: function(httpResponse) {
response.success(httpResponse.text);
},
error: function(httpResponse) {
response.error('Request failed with response ' + httpResponse.status + ' , ' + httpResponse);
}
});
});
Why is this failing?
I'm not sure if the problem is caused by the library or app settings but I'm getting this error. My keys seem to be correct. I'm trying to get home_timeline. Also Application settings access level is - Read, write, and direct messages.
Help would be appreciated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.