Last active
February 23, 2016 23:00
-
-
Save jeduan/a977ae2aeb682ee757e0 to your computer and use it in GitHub Desktop.
Implementing Twitter's OAuth with only request
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
import request from 'request-promise' | |
import {parse as qsparse} from 'querystring' | |
import parse from 'co-body' | |
var post = request.defaults({json: true}).post | |
const TWITTER_KEY = '' | |
const TWITTER_SECRET = '' | |
const TWITTER_CALLBACK_URL = '' | |
export function * requestToken () { | |
var body = yield post({ | |
uri: 'https://api.twitter.com/oauth/request_token', | |
oauth: { | |
consumer_key: TWITTER_KEY, | |
consumer_secret: TWITTER_SECRET, | |
callback: TWITTER_CALLBACK_URL | |
} | |
}) | |
var data = qsparse(body) | |
this.body = { | |
requestToken: data.oauth_token, | |
requestTokenSecret: data.oauth_token_secret | |
} | |
} | |
export function * login () { | |
const body = yield parse(this) | |
this.assert(body.requestToken, 400, 'Require param missing: `request token`') | |
this.assert(body.verifier, 400, 'Required param missing `verifier`') | |
var accessTokenRequest = yield post({ | |
uri: 'https://api.twitter.com/oauth/access_token', | |
oauth: { | |
consumer_key: TWITTER_KEY, | |
consumer_secret: TWITTER_SECRET, | |
token: body.requestToken, | |
verifier: body.verifier | |
} | |
}) | |
var userData = qsparse(accessTokenRequest) | |
/* | |
userData = { | |
user_id, | |
screen_name, | |
oauth_token, | |
oauth_token_secret | |
} | |
*/ | |
// store info on the user here | |
this.body = userData | |
} | |
export function * twitterPost () { | |
var body = yield parse(this) | |
this.assert(body.message, 400, 'Required field message not found') | |
// assume user has oauth_token and oauth_token_secret stored. | |
yield post({ | |
uri: 'https://api.twitter.com/1.1/statuses/update.json', | |
qs : { | |
status : body.message | |
}, | |
oauth: { | |
consumer_key: TWITTER_KEY, | |
consumer_secret: TWITTER_SECRET, | |
token : user.oauth_token, | |
token_secret : user.oauth_token_secret | |
} | |
}) | |
} |
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
import {parse} from 'query-string' | |
var qs = parse(document.location.search) | |
if (qs.oauth_token && qs.oauth_verifier) { | |
var [requestToken, verifier] = [qs.oauth_token, qs.oauth_verifier] | |
fetch(LOGIN_URL, { | |
method: 'POST', | |
body: JSON.stringify({requestToken, verifier}) | |
}) | |
.then((res) => res.json()) | |
.then((data) => console.log(data)) | |
} | |
function twitterLogin () { | |
fetch(REQUEST_TOKEN_URL, { | |
method: 'POST' | |
}) | |
.then(res => res.json()) | |
.then((body) => { | |
document.location.href = `https://api.twitter.com/oauth/authenticate?oauth_token=${body.requestToken}` | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment