Skip to content

Commit 70c337d

Browse files
author
Eric Koleda
authored
Merge pull request #218 from labnol/patch-1
Mailchimp API with Google Apps Script
2 parents a90f497 + 77e95b7 commit 70c337d

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

samples/Mailchimp.gs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This sample demonstrates how to configure the library for the Mailchimp API.
3+
* Instructions on how to generate OAuth credentuals is available here:
4+
* https://mailchimp.com/developer/guides/how-to-use-oauth2/#Step_1%3A_Register_your_application
5+
*/
6+
7+
var CLIENT_ID = '...';
8+
var CLIENT_SECRET = '...';
9+
10+
/**
11+
* Authorizes and makes a request to the Docusign API.
12+
*/
13+
function run() {
14+
var service = getService();
15+
if (service.hasAccess()) {
16+
// Retrieve the account ID and base URI from storage.
17+
var storage = service.getStorage();
18+
var dc = storage.getValue('dc');
19+
20+
// Make a request to retrieve the Mailchimp campaigns.
21+
var url = 'https://' + dc + '.api.mailchimp.com/3.0/campaigns/';
22+
var response = UrlFetchApp.fetch(url, {
23+
headers: {
24+
Authorization: 'Bearer ' + service.getAccessToken()
25+
}
26+
});
27+
var result = JSON.parse(response.getContentText());
28+
Logger.log(JSON.stringify(result, null, 2));
29+
} else {
30+
var authorizationUrl = service.getAuthorizationUrl();
31+
Logger.log('Open the following URL and re-run the script: %s',
32+
authorizationUrl);
33+
}
34+
}
35+
36+
/**
37+
* Reset the authorization state, so that it can be re-tested.
38+
*/
39+
function reset() {
40+
getService().reset();
41+
}
42+
43+
/**
44+
* Configures the service.
45+
*/
46+
function getService() {
47+
return OAuth2.createService('Mailchimp')
48+
// Set the endpoint URLs.
49+
.setAuthorizationBaseUrl('https://login.mailchimp.com/oauth2/authorize')
50+
.setTokenUrl('https://login.mailchimp.com/oauth2/token')
51+
52+
// Set the client ID and secret.
53+
.setClientId(CLIENT_ID)
54+
.setClientSecret(CLIENT_SECRET)
55+
56+
// Set the name of the callback function that should be invoked to
57+
// complete the OAuth flow.
58+
.setCallbackFunction('authCallback')
59+
60+
// Set the property store where authorized tokens should be persisted.
61+
.setPropertyStore(PropertiesService.getUserProperties())
62+
63+
// Set the cache store where authorized tokens should be persisted.
64+
.setCache(CacheService.getUserCache());
65+
};
66+
67+
/**
68+
* Handles the OAuth callback.
69+
*/
70+
function authCallback(request) {
71+
var service = getService();
72+
var authorized = service.handleCallback(request);
73+
if (authorized) {
74+
// Get the user info to determine the data center needed for
75+
// future requests.
76+
var url = 'https://login.mailchimp.com/oauth2/metadata';
77+
var response = UrlFetchApp.fetch(url, {
78+
headers: {
79+
Authorization: 'Bearer ' + service.getAccessToken()
80+
}
81+
});
82+
var result = JSON.parse(response.getContentText());
83+
84+
// Store the Mailchimp datacenter for future API calls.
85+
var storage = service.getStorage();
86+
storage.setValue('dc', result.dc);
87+
88+
return HtmlService.createHtmlOutput('Success!');
89+
} else {
90+
return HtmlService.createHtmlOutput('Denied.');
91+
}
92+
}
93+
94+
/**
95+
* Logs the redict URI to register in the Mailchimp application settings.
96+
*/
97+
function logRedirectUri() {
98+
Logger.log(OAuth2.getRedirectUri());
99+
}

0 commit comments

Comments
 (0)