Skip to content

Instantly share code, notes, and snippets.

@KOBA789
Created August 25, 2010 16:49
Show Gist options
  • Save KOBA789/549836 to your computer and use it in GitHub Desktop.
Save KOBA789/549836 to your computer and use it in GitHub Desktop.
require("./evil");
var cURL = require("./curl").cURL;
exports.TwitterOAuth = function (consumerKey, consumerSecret) {
this.consumerKey = consumerKey;
this.consumerSecret = consumerSecret;
var parent = this;
this.BaseClass = function (token, tokenSecret) {
this.token = token;
this.tokenSecret = tokenSecret;
this.parameter = new Object();
this.url = new String();
this.method = new String();
this.setDefaultParameters = function () {
var nonce = Evil.getNonce(20);
var timestamp = Evil.getTimestamp();
this.parameter = {
oauth_consumer_key: consumerKey,
oauth_nonce: nonce,
oauth_signature_method: "HMAC-SHA1",
oauth_timestamp: timestamp,
oauth_version: "1.0"
};
if (this.token) {
this.parameter["oauth_token"] = this.token;
}
}
this.setPIN = function (PIN) {
this.parameter["oauth_verifier"] = PIN;
}
this.setSignature = function () {
var parameterString = Evil.generateParameter(this.parameter);
var signature = Evil.generateSignature(this.method, this.url, parameterString, parent.consumerSecret, this.tokenSecret);
this.parameter["oauth_signature"] = signature;
return signature;
}
this.execute = function (callback) {
var curl = new cURL(this.url, this.method);
curl.query = this.parameter;
curl.access(callback);
}
}
this.API = function (token, tokenSecret) {
this.update = function (options, callback) {
var base = new parent.BaseClass(token, tokenSecret);
base.setDefaultParameters();
for (var key in options) {
base.parameter[key] = Evil.escapeChars(options[key]);
}
base.url = "http://api.twitter.com/1/statuses/update.json";
base.method = "POST";
base.setSignature();
base.execute(callback);
}
this.homeTimeline = function (options, callback) {
var base = new parent.BaseClass(token, tokenSecret);
base.setDefaultParameters();
for (var key in options) {
base.parameter[key] = Evil.escapeChars(options[key]);
}
base.url = "http://api.twitter.com/1/statuses/home_timeline.json";
base.method = "GET";
base.setSignature();
base.execute(callback);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment