Created
January 29, 2015 04:40
-
-
Save pzurek/2bb2b552bbaf1c62e217 to your computer and use it in GitHub Desktop.
Get token function
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
// This function exchanges the authorization code for a token and returns the parsed info | |
function getToken(myCode, myDomainPrefix) { | |
var tokenUrl = "https://" + myDomainPrefix + ".vendhq.com/api/1.0/token"; | |
var payload = { | |
"code": myCode, | |
"client_id": myVendClientId, | |
"client_secret": myVendClientSecret, | |
"grant_type": "authorization_code", | |
"redirect_uri": myVendRedirectUri | |
}; | |
var options = { | |
"method": "post", | |
"payload": payload | |
}; | |
Logger.log(options); | |
// Make the request | |
var response = UrlFetchApp.fetch(tokenUrl, options); | |
Logger.log(response.getContentText()); | |
// Parse the reponse | |
var data = JSON.parse(response); | |
Logger.log(data); | |
var myAccessToken = data.access_token; | |
var myTokenExpiry = data.expires; | |
var myRefreshToken = data.refresh_token; | |
// Return an array of values | |
return [myAccessToken, myTokenExpiry, myRefreshToken]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment