Last active
December 10, 2019 13:19
-
-
Save jackrobertscott/875b8b1434877666d06e02b1525c1b43 to your computer and use it in GitHub Desktop.
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 axios from 'axios'; | |
import * as queryString from 'query-string'; | |
async function getAccessTokenFromCode(code) { | |
const { data } = await axios({ | |
url: 'https://github.com/login/oauth/access_token', | |
method: 'get', | |
params: { | |
client_id: process.env.APP_ID_GOES_HERE, | |
client_secret: process.env.APP_SECRET_GOES_HERE, | |
redirect_uri: 'https://www.example.com/authenticate/github', | |
code, | |
}, | |
}); | |
/** | |
* GitHub returns data as a string we must parse. | |
*/ | |
const parsedData = queryString.parse(data); | |
console.log(parsedData); // { token_type, access_token, error, error_description } | |
if (parsedData.error) throw new Error(parsedData.error_description) | |
return parsedData.access_token; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment