Skip to content

Commit 978385c

Browse files
authored
Support for Mailchimp API
I have used the OAuth2 library to also add support for Mailchimp in my Mail Merge add-on for Gmail.
1 parent a90f497 commit 978385c

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

samples/Mailchimp.gs

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

0 commit comments

Comments
 (0)