Skip to content

Instantly share code, notes, and snippets.

@portercar
Created July 25, 2017 22:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save portercar/9358196fcf8b7972845bcef8889bc67d to your computer and use it in GitHub Desktop.
Save portercar/9358196fcf8b7972845bcef8889bc67d to your computer and use it in GitHub Desktop.
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