|
| 1 | +/* |
| 2 | + * This sample demonstrates how to configure the library for the HubSpot API. |
| 3 | + * Instructions on how to generate OAuth credentuals is available here: |
| 4 | + * https://developers.hubspot.com/docs/api/oauth-quickstart-guide |
| 5 | + */ |
| 6 | + |
| 7 | +var CLIENT_ID = '...'; |
| 8 | +var CLIENT_SECRET = '...'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Authorizes and makes a request to the HubSpot API. |
| 12 | + */ |
| 13 | +function run() { |
| 14 | + var service = getService(); |
| 15 | + if (service.hasAccess()) { |
| 16 | + // Make a request to retrieve the list of CRM owners. |
| 17 | + var url = 'https://api.hubapi.com/crm/v3/owners/'; |
| 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 | + */ |
| 42 | +function getService() { |
| 43 | + return OAuth2.createService('HubSpot') |
| 44 | + // Set the endpoint URLs. |
| 45 | + .setAuthorizationBaseUrl('https://app.hubspot.com/oauth/authorize') |
| 46 | + .setTokenUrl('https://api.hubapi.com/oauth/v1/token') |
| 47 | + |
| 48 | + // Set the client ID and secret. |
| 49 | + .setClientId(CLIENT_ID) |
| 50 | + .setClientSecret(CLIENT_SECRET) |
| 51 | + |
| 52 | + // Set the name of the callback function that should be invoked to |
| 53 | + // complete the OAuth flow. |
| 54 | + .setCallbackFunction('authCallback') |
| 55 | + |
| 56 | + // Set the property store where authorized tokens should be persisted. |
| 57 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 58 | + |
| 59 | + // Set the scopes to request from the user. The full list of scopes is |
| 60 | + // available here: |
| 61 | + // https://developers.hubspot.com/docs/api/working-with-oauth#scopes |
| 62 | + .setScope('contacts'); |
| 63 | +}; |
| 64 | + |
| 65 | +/** |
| 66 | + * Handles the OAuth callback. |
| 67 | + */ |
| 68 | +function authCallback(request) { |
| 69 | + var service = getService(); |
| 70 | + var authorized = service.handleCallback(request); |
| 71 | + if (authorized) { |
| 72 | + return HtmlService.createHtmlOutput('Success!'); |
| 73 | + } else { |
| 74 | + return HtmlService.createHtmlOutput('Denied.'); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Logs the redirect URI to register. |
| 80 | + */ |
| 81 | +function logRedirectUri() { |
| 82 | + Logger.log(OAuth2.getRedirectUri()); |
| 83 | +} |
0 commit comments