Last active
November 1, 2021 09:46
-
-
Save ChrisRisner/5549984 to your computer and use it in GitHub Desktop.
Twitter API 1.1 Auth With Mobile Services
This file contains hidden or 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
function generateOAuthSignature(method, url, data){ | |
var index = url.indexOf('?'); | |
if (index > 0) | |
url = url.substring(0, url.indexOf('?')); | |
var signingToken = encodeURIComponent('uqZLVntpcEqx1nJ3xNkOzZIcKifKk7053WcZG8n5s') + "&" + encodeURIComponent('naVCZRp7I0gquIVHYyeHlmUPD9ASYVg9bXjhGRa11E'); | |
//encodeURIComponent('Your Consumer Secret') + "&" + encodeURIComponent('Your Access Token Secret'); | |
var keys = []; | |
for (var d in data){ | |
if (d != 'oauth_signature') { | |
console.log('data: ' , d); | |
keys.push(d); | |
} | |
} | |
keys.sort(); | |
var output = "GET&" + encodeURIComponent(url) + "&"; | |
var params = ""; | |
keys.forEach(function(k){ | |
params += "&" + encodeURIComponent(k) + "=" + encodeURIComponent(data[k]); | |
}); | |
params = encodeURIComponent(params.substring(1)); | |
return hashString(signingToken, output+params, "base64"); | |
} |
This file contains hidden or 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
function hashString(key, str, encoding){ | |
var hmac = crypto.createHmac("sha1", key); | |
hmac.update(str); | |
return hmac.digest(encoding); | |
} |
This file contains hidden or 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
var crypto = require('crypto'); | |
var querystring = require('querystring'); | |
function read(query, user, request) { | |
var result = { | |
id: query.id, | |
identities: user.getIdentities(), | |
userName: '' | |
}; | |
//If they used a built in provider, get hte url we should use to get more information | |
var identities = user.getIdentities(); | |
//For twitter we need the twitter ID to use in the URL | |
var userId = user.userId; | |
var twitterId = userId.substring(userId.indexOf(':') + 1); | |
//API 1.0 | |
//url = 'https://api.twitter.com/1/users/show/' + twitterId + '.json'; | |
//API 1.1 | |
var url = 'https://api.twitter.com/1.1/users/show.json?user_id=' + twitterId; | |
var key = '8PDPZfA7KWbXHdDGuSCbeg'; //This is your consumer key | |
var nonce = generateNonce(); | |
var sigmethod = 'HMAC-SHA1'; | |
var version = '1.0'; | |
var twitterAccessToken = identities.twitter.accessToken; | |
var oauth_token = '915248383-qr3pkFBxqt1VRoOC5QlZdEmTBQboiui1YNgE3P3G'; //The Access Token | |
var seconds = new Date() / 1000; | |
seconds = Math.round(seconds); | |
var requestType = 'GET'; | |
var oauthData = { oauth_consumer_key: key, oauth_nonce: nonce, oauth_signature:null, | |
oauth_signature_method: sigmethod, oauth_timestamp: seconds, | |
oauth_token: oauth_token, oauth_version: version }; | |
var sigData = {}; | |
for (var k in oauthData){ | |
sigData[k] = oauthData[k]; | |
} | |
sigData['user_id'] = twitterId; | |
var sig = generateOAuthSignature('GET', url, sigData); | |
oauthData.oauth_signature = sig; | |
var oauthHeader = ""; | |
for (k in oauthData){ | |
oauthHeader += ", " + encodeURIComponent(k) + "=\"" + encodeURIComponent(oauthData[k]) + "\""; | |
} | |
oauthHeader = oauthHeader.substring(1); | |
var authHeader = 'OAuth' + oauthHeader; | |
//Generate callback for response from Twitter API | |
var requestCallback = function (err, resp, body) { | |
if (err || resp.statusCode !== 200) { | |
console.error('Error sending data to the provider: ', err); | |
request.respond(statusCodes.INTERNAL_SERVER_ERROR, body); | |
} else { | |
try { | |
var userData = JSON.parse(body); | |
if (userData.name != null) | |
result.UserName = userData.name; | |
else | |
result.UserName = "can't get username"; | |
request.respond(200, [result]); | |
} catch (ex) { | |
console.error('Error parsing response from the provider API: ', ex); | |
request.respond(statusCodes.INTERNAL_SERVER_ERROR, ex); | |
} | |
} | |
} | |
//Create the request and execute it | |
var req = require('request'); | |
var reqOptions = { | |
uri: url, | |
headers: { Accept: "application/json" } | |
}; | |
if (authHeader != null) | |
reqOptions.headers['Authorization'] = authHeader; | |
req(reqOptions, requestCallback); | |
} | |
function generateOAuthSignature(method, url, data){ | |
var index = url.indexOf('?'); | |
if (index > 0) | |
url = url.substring(0, url.indexOf('?')); | |
var signingToken = encodeURIComponent('uqZLVntpcEqx1nJ3xNkOzZIcKifKk7053WcZG8n5s') + "&" + encodeURIComponent('naVCZRp7I0gquIVHYyeHlmUPD9ASYVg9bXjhGRa11E'); | |
//encodeURIComponent('Your Consumer Secret') + "&" + encodeURIComponent('Your Access Token Secret'); | |
var keys = []; | |
for (var d in data){ | |
if (d != 'oauth_signature') { | |
console.log('data: ' , d); | |
keys.push(d); | |
} | |
} | |
keys.sort(); | |
var output = "GET&" + encodeURIComponent(url) + "&"; | |
var params = ""; | |
keys.forEach(function(k){ | |
params += "&" + encodeURIComponent(k) + "=" + encodeURIComponent(data[k]); | |
}); | |
params = encodeURIComponent(params.substring(1)); | |
return hashString(signingToken, output+params, "base64"); | |
} | |
function hashString(key, str, encoding){ | |
var hmac = crypto.createHmac("sha1", key); | |
hmac.update(str); | |
return hmac.digest(encoding); | |
} | |
function generateNonce() { | |
var code = ""; | |
for (var i = 0; i < 20; i++) { | |
code += Math.floor(Math.random() * 9).toString(); | |
} | |
return code; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment