|
| 1 | +/** |
| 2 | + * This sample demonstrates how to connect to the Zoho CRM API. |
| 3 | + * @see https://www.zoho.com/crm/developer/docs/api/oauth-overview.html |
| 4 | + */ |
| 5 | + |
| 6 | +var CLIENT_ID = '...'; |
| 7 | +var CLIENT_SECRET = '...'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Authorizes and makes a request to the Zoho CRM API. |
| 11 | + */ |
| 12 | +function run() { |
| 13 | + var service = getService(); |
| 14 | + if (service.hasAccess()) { |
| 15 | + // Retrieve the API server from the token. |
| 16 | + var apiServer = service.getToken().api_domain; |
| 17 | + var url = apiServer + '/crm/v2/org'; |
| 18 | + var response = UrlFetchApp.fetch(url, { |
| 19 | + headers: { |
| 20 | + 'Authorization': 'Bearer ' + service.getAccessToken() |
| 21 | + } |
| 22 | + }); |
| 23 | + var result = JSON.parse(response.getContentText()); |
| 24 | + Logger.log(JSON.stringify(result, null, 2)); |
| 25 | + } else { |
| 26 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 27 | + Logger.log('Open the following URL and re-run the script: %s', |
| 28 | + authorizationUrl); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Reset the authorization state, so that it can be re-tested. |
| 34 | + */ |
| 35 | +function reset() { |
| 36 | + getService().reset(); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Configures the service. |
| 41 | + * @param {string} optAccountServer The account server to use when requesting |
| 42 | + * tokens. |
| 43 | + */ |
| 44 | +function getService(optAccountServer) { |
| 45 | + var service = OAuth2.createService('Zoho') |
| 46 | + // Set the authorization base URL. |
| 47 | + .setAuthorizationBaseUrl('https://accounts.zoho.com/oauth/v2/auth') |
| 48 | + |
| 49 | + // Set the client ID and secret. |
| 50 | + .setClientId(CLIENT_ID) |
| 51 | + .setClientSecret(CLIENT_SECRET) |
| 52 | + |
| 53 | + // Set scopes. See |
| 54 | + // https://www.zoho.com/crm/developer/docs/api/oauth-overview.html#scopes. |
| 55 | + .setScope('ZohoCRM.org.all') |
| 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 access type to "offline" to get a refresh token. |
| 62 | + .setParam('access_type', 'offline') |
| 63 | + |
| 64 | + // Set the property store where authorized tokens should be persisted. |
| 65 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 66 | + .setCache(CacheService.getUserCache()); |
| 67 | + |
| 68 | + // Set the token URL using the account server passed in or previously stored. |
| 69 | + var accountServer = optAccountServer || |
| 70 | + service.getStorage().getValue('account-server'); |
| 71 | + if (accountServer) { |
| 72 | + service.setTokenUrl(accountServer + '/oauth/v2/token'); |
| 73 | + } |
| 74 | + return service; |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Handles the OAuth callback. |
| 79 | + */ |
| 80 | +function authCallback(request) { |
| 81 | + var accountServer = request.parameter['accounts-server']; |
| 82 | + var service = getService(accountServer); |
| 83 | + var authorized = service.handleCallback(request); |
| 84 | + if (authorized) { |
| 85 | + // Save the account server in the service's storage. |
| 86 | + service.getStorage().setValue('account-server', accountServer); |
| 87 | + return HtmlService.createHtmlOutput('Success!'); |
| 88 | + } else { |
| 89 | + return HtmlService.createHtmlOutput('Denied.'); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/** |
| 94 | + * Logs the redict URI to register. |
| 95 | + */ |
| 96 | +function logRedirectUri() { |
| 97 | + Logger.log(OAuth2.getRedirectUri()); |
| 98 | +} |
0 commit comments