Last active
February 17, 2017 16:57
-
-
Save fitzk/45c36d6928d489459967912bf6a391c9 to your computer and use it in GitHub Desktop.
Webtask & Twitter application authorization example.
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
/* | |
* docs | |
* Webtask - https://webtask.io | |
* Twitter - https://dev.twitter.com/oauth/application-only | |
* note | |
* Here is an easy way to base64 your consumer key and consumer secret | |
* from the command line: | |
* | |
* echo -n "CONSUMER_KEY:CONSUMER_SECRET" | base64 | |
* | |
*/ | |
'use latest'; | |
import https from "https"; | |
import querystring from "querystring"; | |
module.exports = function (context, response) { | |
const options = { | |
hostname: 'api.twitter.com', | |
path: '/oauth2/token', | |
method: 'POST', | |
headers: { | |
'Authorization': `Basic ${context.secrets.consumer_key_secret_base64}`, | |
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', | |
'Accept':'gzip' | |
} | |
}; | |
const req = https.request(options, (res) => { | |
res.setEncoding('utf8'); | |
let data = ''; | |
res.on('data', (chunk) => { | |
console.log(`body: ${chunk}`); | |
data += chunk | |
}); | |
res.on('end', () => { | |
console.log('response has ended'); | |
response(null, JSON.parse(data)) | |
}); | |
}); | |
req.on('error', (e) => { | |
response(`error: ${e.message}`, null) | |
}); | |
// write grant_type=client_credentials into request body | |
req.write('grant_type=client_credentials'); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment