Last active
April 18, 2016 07:54
-
-
Save Yengas/27cb5b5c836470c7c0e899c989292b1e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const https = require("https"); | |
const qs = require("querystring"); | |
const esc = qs.escape; | |
const crypto = require('crypto'); | |
const HMAC = crypto.createHmac; | |
class twitter { | |
constructor(o) { | |
// if (!o || !o.consumer_key || !o.consumer_secret) throw new Error("Missing Paramaters"); | |
this.id = o.consumer_key; | |
this.secret = o.consumer_secret; | |
} | |
getNonce() { | |
let num = 32; | |
let preDefined = Date.now().toString().split(""); | |
num -= preDefined.length; | |
while (num--) { | |
preDefined.push(Math.round(Math.random() * 31).toString(32)); | |
} | |
return (new Buffer(preDefined.join("")).toString("base64")); | |
} | |
getSignature(HTTPmethod, url, parameters, tokenSecret) { | |
const method = HTTPmethod.toUpperCase(); | |
const baseUrl = url; | |
const params = parameters; | |
const sorted = Object.keys(params).sort(); | |
let baseString = `${esc(method)}&${esc(baseUrl)}`; | |
let signingKey = `${esc(this.secret)}&` | |
signingKey += tokenSecret ? esc(tokenSecret) : ""; | |
let firstRun = true; | |
sorted.forEach(param => { | |
if (firstRun) { | |
baseString += "&"; | |
firstRun = false; | |
} | |
else { | |
baseString += esc("&"); | |
} | |
baseString += esc(`${esc(param)}=${esc(params[param])}`); | |
}); | |
console.log(baseString); | |
const sign = HMAC('SHA1', signingKey).update(baseString).digest('base64'); | |
console.log("------------------------"); | |
console.log(sign); | |
return sign; | |
} | |
getHeaders(httpMethod, baseUrl, additional, token, tokenSecret, extraHeaders) { | |
let headers = { | |
oauth_consumer_key: this.id, | |
oauth_nonce: this.getNonce(), | |
oauth_signature_method: "HMAC-SHA1", | |
oauth_timestamp: Math.floor(Date.now() / 1000), | |
oauth_version: "1.0" | |
} | |
if (extraHeaders) { | |
for (let i in extraHeaders) { | |
headers[i] = extraHeaders[i]; | |
} | |
} | |
if (token) headers.oauth_token = token; | |
let params = headers; | |
if (additional) { | |
for (let i in additional) { | |
params[i] = additional[i]; | |
} | |
} | |
// const signature = this.getSignature(httpMethod, baseUrl, params, tokenSecret || ""); | |
headers.oauth_signature = this.getSignature(httpMethod, baseUrl, params, tokenSecret || ""); | |
let header = `OAuth `; | |
let firstRun = true; | |
const sorted = Object.keys(headers).sort(); | |
sorted.forEach(i => { | |
let prefix; | |
if (firstRun) { | |
prefix = ""; | |
firstRun = false; | |
} | |
else { | |
prefix = ", "; | |
} | |
header += `${prefix}${esc(i)}="${esc(headers[i])}"` | |
}); | |
// header += `, oauth_signature="${esc(signature)}"`; | |
return header; | |
} | |
getRequestToken(cb) { | |
// if (!cb) throw new Error('callback must be defined'); | |
const callbackUrl = cb||""; | |
let headers = this.getHeaders("POST", "https://api.twitter.com/oauth/request_token", false, false, false, { | |
oauth_callback: callbackUrl | |
}); | |
const reqParams = { | |
method: "POST", | |
host: "api.twitter.com", | |
path: "/oauth/request_token", | |
headers: { "Authorization": headers } | |
} | |
const req = https.request(reqParams, res => { | |
let data = ""; | |
res.on("data", d => data += d); | |
res.on("end", _ => console.log(data)); | |
}); req.end(); | |
} | |
} | |
let a = (new twitter({ | |
consumer_key: "xx", | |
consumer_secret: "xx" | |
})) | |
// .getHeaders("post", "https://api.twitter.com/request_token"); | |
.getRequestToken("http://127.0.0.1/twitter"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment