|
| 1 | +/** |
| 2 | + * Demonstrates how to authorize access to the RingCentral API using the sandbox |
| 3 | + * environment. |
| 4 | + * @see http://ringcentral-api-docs.readthedocs.io/en/latest/oauth/ |
| 5 | + */ |
| 6 | + |
| 7 | +var CLIENT_ID = '...'; |
| 8 | +var CLIENT_SECRET = '...'; |
| 9 | + |
| 10 | +// The server to send requests to, currently the sandbox. |
| 11 | +var SERVER = 'https://platform.devtest.ringcentral.com'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Authorizes and makes a request to the RingCentral API. |
| 15 | + */ |
| 16 | +function run() { |
| 17 | + var service = getService(); |
| 18 | + if (service.hasAccess()) { |
| 19 | + var url = SERVER + '/restapi/v1.0/account/~'; |
| 20 | + var response = UrlFetchApp.fetch(url, { |
| 21 | + headers: { |
| 22 | + 'Authorization': 'Bearer ' + service.getAccessToken() |
| 23 | + } |
| 24 | + }); |
| 25 | + var result = JSON.parse(response.getContentText()); |
| 26 | + Logger.log(JSON.stringify(result, null, 2)); |
| 27 | + } else { |
| 28 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 29 | + Logger.log('Open the following URL and re-run the script: %s', |
| 30 | + authorizationUrl); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Reset the authorization state, so that it can be re-tested. |
| 36 | + */ |
| 37 | +function reset() { |
| 38 | + getService().reset(); |
| 39 | +} |
| 40 | + |
| 41 | +/** |
| 42 | + * Configures the service. |
| 43 | + */ |
| 44 | +function getService() { |
| 45 | + return OAuth2.createService('RingCentral') |
| 46 | + // Set the endpoint URLs. |
| 47 | + .setAuthorizationBaseUrl(SERVER + '/restapi/oauth/authorize') |
| 48 | + .setTokenUrl(SERVER + '/restapi/oauth/token') |
| 49 | + |
| 50 | + // Set the client ID and secret. |
| 51 | + .setClientId(CLIENT_ID) |
| 52 | + .setClientSecret(CLIENT_SECRET) |
| 53 | + |
| 54 | + // Set the name of the callback function that should be invoked to |
| 55 | + // complete the OAuth flow. |
| 56 | + .setCallbackFunction('authCallback') |
| 57 | + |
| 58 | + // Set the property store where authorized tokens should be persisted. |
| 59 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 60 | + |
| 61 | + // Set the required authorization header. |
| 62 | + .setTokenHeaders({ |
| 63 | + 'Authorization': 'Basic ' + |
| 64 | + Utilities.base64EncodeWebSafe(CLIENT_ID + ':' + CLIENT_SECRET) |
| 65 | + }); |
| 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 | + return HtmlService.createHtmlOutput('Success!'); |
| 76 | + } else { |
| 77 | + return HtmlService.createHtmlOutput('Denied'); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/** |
| 82 | + * Logs the redict URI to register. |
| 83 | + */ |
| 84 | +function logRedirectUri() { |
| 85 | + Logger.log(getService().getRedirectUri()); |
| 86 | +} |
0 commit comments