Created
November 29, 2018 19:09
-
-
Save manzanit0/890c276ec02976b60245b759d9a3ce41 to your computer and use it in GitHub Desktop.
Snippet to get access tokens from Google Identity API, given a refresh token.
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
const buildAccessTokenUri = (refreshToken, clientId, clientSecret) => | |
`https://www.googleapis.com/oauth2/v4/token?client_id=${clientId}&client_secret=${clientSecret}&refresh_token=${refreshToken}&grant_type=refresh_token`; | |
const getAccessToken = async (refreshToken, clientId, clientSecret) => { | |
accessTokenUri = buildAccessTokenUri(refreshToken, clientId, clientSecret); | |
const rawResponse = await fetch(accessTokenUri, { method: 'POST' }); | |
const jsonResponse = await rawResponse.json(); | |
return jsonResponse.access_token; | |
}; | |
/* | |
* Main Function | |
*/ | |
(async() => { | |
const refreshToken = 'REFRESH TOKEN'; | |
const clientId = 'CLIENT ID'; | |
const clientSecret = 'CLIENT SECRET'; | |
const accessToken = await getAccessToken(refreshToken, clientId, clientSecret); | |
console.log(`Access token: ${accessToken}`); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment