Skip to content

Instantly share code, notes, and snippets.

@flovilmart
Last active August 29, 2015 13:58
Show Gist options
  • Save flovilmart/10338206 to your computer and use it in GitHub Desktop.
Save flovilmart/10338206 to your computer and use it in GitHub Desktop.

HOW TO USE


var Twitter = require("cloud/Twitter.parse.js");

// a user that has logged in with twitter
var user = Parse.User.current();

// Works with that object if colledted otherwise :)
var authData = {
	consumer_key: "",
	consumer_secret: "",
	auth_token: "",
	auth_token_secret:"",
};

var twitter = new Twitter(user);
// query parameters for the API
var params = {};
// body of the request
var body = {status: "I'm using Twitter API on Cloud Code!"}

twitter.post("/statuses/update.json", params, body)
.then(function(httpResponse){
	// YAY!
}, function(error){
	// OOPs
});
var _ = require("underscore");
OAuth = {};
/*
Proper string %escape encoding
*/
OAuth.encode = function(str) {
// discuss at: http://phpjs.org/functions/rawurlencode/
// original by: Brett Zamir (http://brett-zamir.me)
// input by: travc
// input by: Brett Zamir (http://brett-zamir.me)
// input by: Michael Grier
// input by: Ratheous
// bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// bugfixed by: Brett Zamir (http://brett-zamir.me)
// bugfixed by: Joris
// reimplemented by: Brett Zamir (http://brett-zamir.me)
// reimplemented by: Brett Zamir (http://brett-zamir.me)
// note: This reflects PHP 5.3/6.0+ behavior
// note: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
// note: pages served as UTF-8
// example 1: rawurlencode('Kevin van Zonneveld!');
// returns 1: 'Kevin%20van%20Zonneveld%21'
// example 2: rawurlencode('http://kevin.vanzonneveld.net/');
// returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
// example 3: rawurlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
// returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
str = (str + '')
.toString();
// Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
// PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
return encodeURIComponent(str)
.replace(/!/g, '%21')
.replace(/'/g, '%27')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29')
.replace(/\*/g, '%2A');
}
OAuth.signatureMethod = "HMAC-SHA1";
OAuth.version = "1.0";
/*
Generate a nonce
*/
OAuth.nonce = function(){
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < 30; i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
OAuth.buildParameterString = function(obj){
var result = {};
// Sort keys and encode values
_.each(_.keys(obj).sort(), function(key){
result[key] = OAuth.encode(obj[key]);
});
obj = result;
// Map key=value, join them by &
return _.map(obj, function(value, key){
return key+"="+value;
}).join("&");
}
/*
Build the signature string from the object
*/
OAuth.buildSignatureString = function(method, url, parameters){
return [method.toUpperCase(), OAuth.encode(url), OAuth.encode(parameters)].join("&");
}
/*
Retuns encoded HMAC-SHA1 from key and text
*/
OAuth.signature = function(text, key){
crypto = require("crypto");
return OAuth.encode(crypto.createHmac('sha1', key).update(text).digest('base64'));
}
/*
Signs the current request
request is a Parse.Cloud.HTTPOptions: http://parse.com/docs/js/symbols/Parse.Cloud.HTTPOptions.html
oauth_parameters require: oauth_consumer_key and oauth_token to be set
consumer_secret
auth_token_secret
*/
OAuth.signRequest = function(request, oauth_parameters, consumer_secret, auth_token_secret){
// Set default values
if (!oauth_parameters.oauth_nonce) {
oauth_parameters.oauth_nonce = OAuth.nonce();
}
if (!oauth_parameters.oauth_timestamp) {
oauth_parameters.oauth_timestamp = Math.floor(new Date().getTime()/1000);
}
if (!oauth_parameters.oauth_signature_method) {
oauth_parameters.oauth_signature_method = OAuth.signatureMethod;
}
if (!oauth_parameters.oauth_version) {
oauth_parameters.oauth_version = OAuth.version;
}
// Force GET method if unset
if (!request.method) {
request.method = "GET"
}
// Collect all the parameters in one signatureParameters object
var signatureParams = {};
_.extend(signatureParams, request.params, request.body, oauth_parameters);
console.log(signatureParams);
// Create a string based on the parameters
var parameterString = OAuth.buildParameterString(signatureParams);
// Build the signature string
var signatureString = OAuth.buildSignatureString(request.method, request.url, parameterString);
// Hash the signature string
var signature = OAuth.signature(signatureString, [OAuth.encode(consumer_secret), OAuth.encode(auth_token_secret)].join("&"));
// Set the signature in the params
oauth_parameters.oauth_signature = signature;
if(!request.headers){
request.headers = {};
}
// Set the authorization header
request.headers.Authorization = ['OAuth', _.map(oauth_parameters, function(v,k){ return k+'="'+v+'"'})].join(" ");
// Set the content type header
request.headers["Content-Type"] = "application/x-www-form-urlencoded";
return request;
}
/*
Create a new instance with a user or authData parameters
*/
Twitter = function(object){
// Check if that's a user
this.authData = null;
this.baseURL = "https://api.twitter.com/1.1/"
this.OAuth = OAuth;
if (typeof object.get == "function") {
// We have a user
this.authData = object.get("authData").twitter;
}else if(typeof object == "object"){
// this is an object
this.authData = object;
}
if(!this.authData){
return null;
}
this.consumer_key = this.authData.consumer_key;
this.consumer_secret = this.authData.consumer_secret;
this.auth_token = this.authData.auth_token;
this.auth_token_secret = this.authData.auth_token_secret;
}
Twitter.prototype.send = function(method, endpoint, params, body){
// Dumbproofing
if (endpoint.indexOf("/") == 0) {
endpoint = endpoint.slice(1,endpoint.length)
}
var request = {
url: this.baseURL+endpoint,
method: method
};
if (Object.keys(params).length>0) {
request.params = params;
}
if (Object.keys(body).length>0) {
request.body = body;
}
var oauth_params = {
"oauth_consumer_key" :this.consumer_key,
"oauth_token" :this.auth_token,
}
request = OAuth.signRequest(request, oauth_params, this.consumer_secret, this.auth_token_secret);
console.log(oauth_params);
// Encode the body properly, the current Parse Implementation don't do it properly
request.body = OAuth.buildParameterString(request.body);
console.log(request);
return Parse.Cloud.httpRequest(request);
}
Twitter.prototype.get = function(endpoint, params, body){
return this.send("GET", endpoint, params, body);
}
Twitter.prototype.post = function(endpoint, params, body){
return this.send("POST", endpoint, params, body);
}
module.exports= Twitter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment