Created
July 25, 2017 22:20
-
-
Save portercar/9358196fcf8b7972845bcef8889bc67d to your computer and use it in GitHub Desktop.
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
function main() { | |
Logger.clear(); | |
var apiKey = ''; | |
var acctID = ''; | |
var token = retrieveTokenByApiKey(apiKey); | |
url = 'https://api.wildapricot.org/v2/accounts/acctID/' | |
var batchUrl = "https://api.wildapricot.org/batch"; | |
var batchData = | |
[{ | |
"Id": "get all available accounts", | |
"Order": 0, | |
"PathAndQuery": "/v2.1/accounts/", | |
"Method": "GET", | |
} | |
, | |
{ | |
"Id": "create new contact", | |
"Order": 1, | |
"PathAndQuery": "/v2/accounts/236510/contacts", | |
"Method": "POST", | |
"Payload": "{\"FirstName\":\"Justin\"}", | |
"ContentType": "application/json" | |
} | |
]; | |
var jsonData = JSON.stringify(batchData); | |
var response = executeBatchRequest(token,batchUrl,jsonData); | |
Logger.log(JSON.stringify(response, null, "\t")); | |
} | |
function retrieveTokenByApiKey(apiKey) | |
{ | |
var authServiceUrl = 'https://oauth.wildapricot.org/auth/token'; | |
var scopeNames = 'auto'; | |
var authRequestParams = { | |
method:'POST', | |
headers:{ | |
Authorization:'Basic ' + Utilities.base64Encode('APIKEY' + ':' + apiKey) | |
}, | |
contentType: "application/x-www-form-urlencoded", | |
payload: Utilities.formatString('grant_type=%s&scope=%s', 'client_credentials', scopeNames) | |
}; | |
var tokenJson = UrlFetchApp.fetch(authServiceUrl, authRequestParams); | |
var tokenData = JSON.parse(tokenJson); | |
return tokenData.access_token; | |
} | |
function executeBatchRequest(token,batchUrl,jsonData) | |
{ | |
var requestParams = { | |
method: 'POST', | |
headers: { Authorization:'Bearer ' + token }, | |
accept:'application/json', | |
contentType: 'application/json', | |
payload: jsonData | |
} | |
var responseJson = UrlFetchApp.fetch(batchUrl, requestParams); | |
var result = JSON.parse(responseJson); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment