To connect you will need to have a MailJet account and your own API/Secret keys. In this example we will connect with the 'Contact List' aspect of the API. We need to create an encoded string which contains our API keys:
var encoding = Utilities.base64Encode(apiKey + ":" + secretKey);From here we can set the HTTP headers authentication as part of the request, to authorise our account details:
var options = {Then we can continue with the usual route of using the 'UrlFetchApp' to make the request and return our data.
'headers': {"Authorization": "Basic " + encoding},
};
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
/* | |
Function to connect with MailJet API. | |
*/ | |
var apiKey = ""; | |
var secretKey = ""; | |
function callAPI() { | |
var url = "https://api.mailjet.com/v3/REST/contactslist/"; | |
//set authentication and get apiKeys ************************************* | |
var encoding = Utilities.base64Encode(apiKey + ":" + secretKey); | |
var options = { | |
'headers': {"Authorization": "Basic " + encoding}, | |
}; | |
//set authentication and get apiKeys ************************************* | |
// fetch API Url with authentication | |
var response = UrlFetchApp.fetch(url, options); | |
// log returned Response code to check for errors | |
var code = response.getResponseCode(); | |
Logger.log('Response code is: ' + code); | |
// log returned data | |
var result = response.getContentText(); | |
Logger.log(result); | |
var text = JSON.parse(result); | |
Logger.log(text); | |
} |
Could I be so bold to ask, how would you save this data in a Google Sheet?
ReplyDeleteTHANK YOU!!!!
Hi
Deletethat was my end goal and unfortunately it was not possible (at least at the time). I was in touch with MailJet support to fix a number of problems with their API but ultimately Apps Script is not 'supported' and so I couldn't get it to work .... sorry.
Kind regards
Phil
Thank you!!!
Delete