Skip to content

Instantly share code, notes, and snippets.

@portercar
Last active May 24, 2017 21:03
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/945c29474fece0e208991ac27f724203 to your computer and use it in GitHub Desktop.
Save portercar/945c29474fece0e208991ac27f724203 to your computer and use it in GitHub Desktop.
function main() {
Logger.clear();
var apiKey = "your apikey here";
var urls = urlBuilder();
var token = retrieveTokenByApiKey(urls.getAuthServiceUrl(), apiKey);
var accountId = getDataFromApi(urls.getAccountsListUrl(), token)[0].Id;
// createdContact = createNewContact(urls,token,accountId);
// Logger.log(createdContact);
createdEvent = createNewEvent(urls,token,accountId);
Logger.log(createdEvent);
}
// This works
function createNewContact(urls,token,accountId)
{
var newContact =
{
"FirstName": "First Test",
"LastName": "Last Test"
};
var createdContact = executeApiRequest(urls.getContactsListUrl(accountId),token,'POST', newContact);
Logger.log(createdContact);
}
// This event data below works in the API browser, just not here.
// This returns a 403 response code
function createNewEvent(urls,token,accountId)
{
var newEvent =
{
"StartDate": "2017-07-05T10:00:00+03:00",
"EndDate": "2017-07-11T18:00:00+03:00",
"Location": "Boston Hilton",
"RegistrationEnabled": false,
"RegistrationsLimit": 100,
"StartTimeSpecified": false,
"EndTimeSpecified": false,
"Name": "Annual General Meeting",
"Tags": [
"meeting",
"agm"
],
"Details": {
"DescriptionHtml": "Join us at our annual general meeting",
"PaymentInstructions": "Please pay online",
"AccessControl": {
"AccessLevel": "Public"
},
"GuestRegistrationSettings": {
"CreateContactMode": "CreateContactForAllGuests"
},
"Organizer": null,
"PaymentMethod": "OnlineOnly",
"RegistrationConfirmationExtraInfo": "additional info for confirmation email",
"RegistrationMessage": "additional info for registration wizard",
"SendEmailCopy": true,
"WaitListBehaviour": "RequestNameAndEmail"
}
};
var createdEvent = executeApiRequest(urls.getEventsListUrl(accountId),token,'POST', newEvent);
Logger.log(createdEvent);
}
function getDataFromApi(url, token)
{
return executeApiRequest(url, token, 'GET');
}
function executeApiRequest(url, token, method, data)
{
if (typeof method === 'undefined') { method = 'GET'; }
var requestParams = {
method:method,
headers: { Authorization:'Bearer ' + token },
accept:'application/json'
}
if( data )
{
requestParams.payload = JSON.stringify(data);
requestParams.contentType = 'application/json';
}
var responseJson = UrlFetchApp.fetch(url, requestParams);
var result = JSON.parse(responseJson);
return result;
}
function retrieveTokenByApiKey(authServiceUrl, apiKey)
{
var scopeNames = "contacts finances events event_registrations account membership_levels";
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 urlBuilder()
{
var apiUrl = 'https://api.wildapricot.org';
return {
getAuthServiceUrl: function()
{
return 'https://oauth.wildapricot.org/auth/token';
},
getAccountsListUrl: function()
{
return apiUrl+'/v2/accounts';
},
getContactFieldsUrl: function(accountId)
{
return apiUrl+'/v2/accounts/' + accountId + '/contactfields';
},
getContactsListUrl: function(accountId)
{
return apiUrl+'/v2/accounts/' + accountId + '/contacts';
},
getEventsListUrl: function(accountId)
{
return apiUrl+'/v2/accounts/' + accountId + '/events';
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment