From 978385cd63fba1acbd332bdae2bff7bcd7d00163 Mon Sep 17 00:00:00 2001 From: Amit Agarwal Date: Sun, 23 Feb 2020 13:58:06 +0530 Subject: [PATCH 1/4] Support for Mailchimp API I have used the OAuth2 library to also add support for Mailchimp in my Mail Merge add-on for Gmail. --- samples/Mailchimp.gs | 100 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 samples/Mailchimp.gs diff --git a/samples/Mailchimp.gs b/samples/Mailchimp.gs new file mode 100644 index 00000000..2bc9f83d --- /dev/null +++ b/samples/Mailchimp.gs @@ -0,0 +1,100 @@ +/* + * This sample demonstrates how to configure the library for the Mailchimp API. + * Instructions on how to generate OAuth credentuals is available here: + * https://mailchimp.com/developer/guides/how-to-use-oauth2/#Step_1%3A_Register_your_application + */ + +var CLIENT_ID = '...'; +var CLIENT_SECRET = '...'; +var OAUTH_HOST = 'login.mailchimp.com'; + +/** + * Authorizes and makes a request to the Docusign API. + */ +function run() { + var service = getService(); + if (service.hasAccess()) { + // Retrieve the account ID and base URI from storage. + var storage = service.getStorage(); + var dc = storage.getValue('dc'); + + // Make a request to retrieve the account information. + var url = 'https://' + dc + '.api.mailchimp.com/3.0/campaigns/'; + var response = UrlFetchApp.fetch(url, { + headers: { + Authorization: 'Bearer ' + service.getAccessToken() + } + }); + var result = JSON.parse(response.getContentText()); + Logger.log(JSON.stringify(result, null, 2)); + } else { + var authorizationUrl = service.getAuthorizationUrl(); + Logger.log('Open the following URL and re-run the script: %s', + authorizationUrl); + } +} + +/** + * Reset the authorization state, so that it can be re-tested. + */ +function reset() { + getService().reset(); +} + +/** + * Configures the service. + */ +function getService() { + return OAuth2.createService('Mailchimp') + // Set the endpoint URLs. + .setAuthorizationBaseUrl('https://' + OAUTH_HOST + '/oauth2/authorize') + .setTokenUrl('https://' + OAUTH_HOST + '/oauth2/token') + + // Set the client ID and secret. + .setClientId(CLIENT_ID) + .setClientSecret(CLIENT_SECRET) + + // Set the name of the callback function that should be invoked to + // complete the OAuth flow. + .setCallbackFunction('authCallback') + + // Set the property store where authorized tokens should be persisted. + .setPropertyStore(PropertiesService.getUserProperties()) + + // Set the cache store where authorized tokens should be persisted. + .setCache(CacheService.getUserCache()) +}; + +/** + * Handles the OAuth callback. + */ +function authCallback(request) { + var service = getService(); + var authorized = service.handleCallback(request); + if (authorized) { + // Get the user info to determine the data center needed for + // future requests. + var url = 'https://' + OAUTH_HOST + '/oauth2/metadata'; + var response = UrlFetchApp.fetch(url, { + headers: { + Authorization: 'Bearer ' + service.getAccessToken() + } + }); + var result = JSON.parse(response.getContentText()); + + // Store the Mailchimp datacenter for future API calls. + var storage = service.getStorage(); + storage.setValue('dc', result.dc); + + return HtmlService.createHtmlOutput('Success!'); + } else { + return HtmlService.createHtmlOutput('Denied.'); + } +} + +/** + * Logs the redict URI to register in the Mailchimp application settings. + */ +function logRedirectUri() { + Logger.log(OAuth2.getRedirectUri()); +} From 0893c8c053fe5f13aa3c15d5e696a8af4b714d9c Mon Sep 17 00:00:00 2001 From: Amit Agarwal Date: Sun, 23 Feb 2020 14:14:03 +0530 Subject: [PATCH 2/4] Fixing ESLint Errors 20:1 error Trailing spaces not allowed no-trailing-spaces 65:43 error Missing semicolon semi 88:1 error Trailing spaces not allowed no-trailing-spaces --- samples/Mailchimp.gs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/Mailchimp.gs b/samples/Mailchimp.gs index 2bc9f83d..f4d8a4bf 100644 --- a/samples/Mailchimp.gs +++ b/samples/Mailchimp.gs @@ -17,7 +17,7 @@ function run() { // Retrieve the account ID and base URI from storage. var storage = service.getStorage(); var dc = storage.getValue('dc'); - + // Make a request to retrieve the account information. var url = 'https://' + dc + '.api.mailchimp.com/3.0/campaigns/'; var response = UrlFetchApp.fetch(url, { @@ -62,7 +62,7 @@ function getService() { .setPropertyStore(PropertiesService.getUserProperties()) // Set the cache store where authorized tokens should be persisted. - .setCache(CacheService.getUserCache()) + .setCache(CacheService.getUserCache()); }; /** @@ -85,7 +85,7 @@ function authCallback(request) { // Store the Mailchimp datacenter for future API calls. var storage = service.getStorage(); storage.setValue('dc', result.dc); - + return HtmlService.createHtmlOutput('Success!'); } else { return HtmlService.createHtmlOutput('Denied.'); From 3354c8a52e04eaafeea8515c82529c7abd7a53c4 Mon Sep 17 00:00:00 2001 From: Amit Agarwal Date: Sun, 23 Feb 2020 20:49:49 +0530 Subject: [PATCH 3/4] Update Mailchimp.gs --- samples/Mailchimp.gs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/Mailchimp.gs b/samples/Mailchimp.gs index f4d8a4bf..d9b09e8b 100644 --- a/samples/Mailchimp.gs +++ b/samples/Mailchimp.gs @@ -18,7 +18,7 @@ function run() { var storage = service.getStorage(); var dc = storage.getValue('dc'); - // Make a request to retrieve the account information. + // Make a request to retrieve the Mailchimp campaigns. var url = 'https://' + dc + '.api.mailchimp.com/3.0/campaigns/'; var response = UrlFetchApp.fetch(url, { headers: { From 77e95b7beafd6abe7e751a739881113921e66306 Mon Sep 17 00:00:00 2001 From: Amit Agarwal Date: Sun, 23 Feb 2020 20:52:40 +0530 Subject: [PATCH 4/4] Update Mailchimp.gs --- samples/Mailchimp.gs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/samples/Mailchimp.gs b/samples/Mailchimp.gs index d9b09e8b..aa7d47d0 100644 --- a/samples/Mailchimp.gs +++ b/samples/Mailchimp.gs @@ -6,7 +6,6 @@ var CLIENT_ID = '...'; var CLIENT_SECRET = '...'; -var OAUTH_HOST = 'login.mailchimp.com'; /** * Authorizes and makes a request to the Docusign API. @@ -47,8 +46,8 @@ function reset() { function getService() { return OAuth2.createService('Mailchimp') // Set the endpoint URLs. - .setAuthorizationBaseUrl('https://' + OAUTH_HOST + '/oauth2/authorize') - .setTokenUrl('https://' + OAUTH_HOST + '/oauth2/token') + .setAuthorizationBaseUrl('https://login.mailchimp.com/oauth2/authorize') + .setTokenUrl('https://login.mailchimp.com/oauth2/token') // Set the client ID and secret. .setClientId(CLIENT_ID) @@ -74,7 +73,7 @@ function authCallback(request) { if (authorized) { // Get the user info to determine the data center needed for // future requests. - var url = 'https://' + OAUTH_HOST + '/oauth2/metadata'; + var url = 'https://login.mailchimp.com/oauth2/metadata'; var response = UrlFetchApp.fetch(url, { headers: { Authorization: 'Bearer ' + service.getAccessToken()