forked from googleworkspace/apps-script-oauth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZohoCRM.gs
100 lines (88 loc) · 2.97 KB
/
ZohoCRM.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* This sample demonstrates how to connect to the Zoho CRM API.
* @see https://www.zoho.com/crm/developer/docs/api/oauth-overview.html
*/
var CLIENT_ID = '...';
var CLIENT_SECRET = '...';
/**
* Authorizes and makes a request to the Zoho CRM API.
*/
function run() {
var service = getService_();
if (service.hasAccess()) {
// Retrieve the API server from the token.
var apiServer = service.getToken().api_domain;
var url = apiServer + '/crm/v2/org';
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.
* @param {string} optAccountServer The account server to use when requesting
* tokens.
*/
function getService_(optAccountServer) {
var service = OAuth2.createService('Zoho')
// Set the authorization base URL.
.setAuthorizationBaseUrl('https://accounts.zoho.com/oauth/v2/auth')
// Set the client ID and secret.
.setClientId(CLIENT_ID)
.setClientSecret(CLIENT_SECRET)
// Set scopes. See
// https://www.zoho.com/crm/developer/docs/api/oauth-overview.html#scopes.
.setScope('ZohoCRM.org.all')
// Set the name of the callback function that should be invoked to
// complete the OAuth flow.
.setCallbackFunction('authCallback')
// Set the access type to "offline" to get a refresh token.
.setParam('access_type', 'offline')
// Set prompt to "consent" to ensure a refresh token is retrieved.
.setParam('prompt', 'consent')
// Set the property store where authorized tokens should be persisted.
.setPropertyStore(PropertiesService.getUserProperties())
.setCache(CacheService.getUserCache());
// Set the token URL using the account server passed in or previously stored.
var accountServer = optAccountServer ||
service.getStorage().getValue('account-server');
if (accountServer) {
service.setTokenUrl(accountServer + '/oauth/v2/token');
}
return service;
}
/**
* Handles the OAuth callback.
*/
function authCallback(request) {
var accountServer = request.parameter['accounts-server'];
var service = getService_(accountServer);
var authorized = service.handleCallback(request);
if (authorized) {
// Save the account server in the service's storage.
service.getStorage().setValue('account-server', accountServer);
return HtmlService.createHtmlOutput('Success!');
} else {
return HtmlService.createHtmlOutput('Denied.');
}
}
/**
* Logs the redict URI to register.
*/
function logRedirectUri() {
Logger.log(OAuth2.getRedirectUri());
}