This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
forked from ciaranj/node-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ciaranj#272 from prometheansacrifice/master
Adds twitter as example for OAuth 1
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
var http = require('http'); | ||
var OAuth = require('../lib/oauth.js').OAuth; | ||
var nodeUrl = require('url'); | ||
var clientID = ''; | ||
var clientSecret = ''; | ||
var callbackURL = ''; | ||
|
||
oa = new OAuth( | ||
'https://api.twitter.com/oauth/request_token', | ||
'https://api.twitter.com/oauth/access_token', | ||
clientID, | ||
clientSecret, | ||
'1.0', | ||
callbackURL, | ||
'HMAC-SHA1' | ||
); | ||
|
||
http.createServer(function (request, response) { | ||
oa.getOAuthRequestToken(function (error, oAuthToken, oAuthTokenSecret, results) { | ||
var urlObj = nodeUrl.parse(request.url, true); | ||
var authURL = 'https://twitter.com/' + | ||
'oauth/authenticate?oauth_token=' + oAuthToken; | ||
var handlers = { | ||
'/': function (request, response) { | ||
/** | ||
* Creating an anchor with authURL as href and sending as response | ||
*/ | ||
var body = '<a href="' + authURL + '"> Get Code </a>'; | ||
response.writeHead(200, { | ||
'Content-Length': body.length, | ||
'Content-Type': 'text/html' }); | ||
response.end(body); | ||
}, | ||
'/callback': function (request, response) { | ||
/** Obtaining access_token */ | ||
var getOAuthRequestTokenCallback = function (error, oAuthAccessToken, | ||
oAuthAccessTokenSecret, results) { | ||
if (error) { | ||
console.log(error); | ||
response.end(JSON.stringify({ | ||
message: 'Error occured while getting access token', | ||
error: error | ||
})); | ||
return; | ||
} | ||
|
||
oa.get('https://api.twitter.com/1.1/account/verify_credentials.json', | ||
oAuthAccessToken, | ||
oAuthAccessTokenSecret, | ||
function (error, twitterResponseData, result) { | ||
if (error) { | ||
console.log(error) | ||
res.end(JSON.stringify(error)); | ||
return; | ||
} | ||
try { | ||
console.log(JSON.parse(twitterResponseData)); | ||
} catch (parseError) { | ||
console.log(parseError); | ||
} | ||
console.log(twitterResponseData); | ||
response.end(twitterResponseData); | ||
}); | ||
}; | ||
|
||
oa.getOAuthAccessToken(urlObj.query.oauth_token, oAuthTokenSecret, | ||
urlObj.query.oauth_verifier, | ||
getOAuthRequestTokenCallback); | ||
|
||
} | ||
}; | ||
handlers[urlObj.pathname](request, response); | ||
}) | ||
|
||
}).listen(3000); |