|
| 1 | +/********************************************** |
| 2 | + * Update of /apps-script-oauth1/.../samples/QuickBooks.gs |
| 3 | + * to support OAuth2. |
| 4 | + ********************************************** |
| 5 | +
|
| 6 | +/** |
| 7 | + * Populate constants with data specific to your application, |
| 8 | + * found at https://developer.intuit.com/v2/ui#/app/appdetail/XXXXXXX |
| 9 | + */ |
| 10 | +var CLIENT_ID = '...'; |
| 11 | +var CLIENT_SECRET = '...'; |
| 12 | + |
| 13 | +// URLS found at Quickbooks Devloper Portal: https://developer.intuit.com |
| 14 | +var BASE_AUTH_URL = 'https://appcenter.intuit.com/connect/oauth2'; |
| 15 | +var TOKEN_URL = 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Log the redirect URI to be pasted in the Intuit Dev Center: |
| 19 | + * https://developer.intuit.com/v2/ui#/app/<YOURAPPID>/keys |
| 20 | + */ |
| 21 | +function logRedirectUri() { |
| 22 | + Logger.log(getService().getRedirectUri()); |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Authorizes and makes a request to the QuickBooks API. Assumes the use of a |
| 27 | + * sandbox company. |
| 28 | + */ |
| 29 | +function run() |
| 30 | +{ |
| 31 | + var service = getService(); |
| 32 | + if (service.hasAccess()) |
| 33 | + { |
| 34 | + // Get the Company ID to be used in the request. |
| 35 | + var companyId = PropertiesService.getUserProperties() |
| 36 | + .getProperty('QuickBooks.companyId'); |
| 37 | + |
| 38 | + // Get company information as a test. |
| 39 | + var url = 'https://sandbox-quickbooks.api.intuit.com/v3/company/' |
| 40 | + + companyId + '/companyinfo/' + companyId; |
| 41 | + |
| 42 | + var response = UrlFetchApp.fetch(url, { |
| 43 | + headers: { Authorization: 'Bearer ' + service.getAccessToken(), |
| 44 | + Accept: 'application/json'} |
| 45 | + }); |
| 46 | + |
| 47 | + var result = JSON.parse(response.getContentText()); |
| 48 | + Logger.log(JSON.stringify(result, null, 2)); |
| 49 | + |
| 50 | + } |
| 51 | + else { |
| 52 | + // If not authorized, get authorization URL. |
| 53 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 54 | + |
| 55 | + Logger.log('Open the following URL and re-run the script: %s', |
| 56 | + authorizationUrl); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Reset the authorization state, so that it can be re-tested. |
| 62 | + */ |
| 63 | +function reset() { |
| 64 | + var service = getService(); |
| 65 | + service.reset(); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Configures the service. |
| 70 | + */ |
| 71 | +function getService() { |
| 72 | + return OAuth2.createService('Quickbooks') |
| 73 | + // Set the endpoint URLs. |
| 74 | + .setAuthorizationBaseUrl(BASE_AUTH_URL) |
| 75 | + .setTokenUrl(TOKEN_URL) |
| 76 | + // Set the client ID and secret. |
| 77 | + .setClientId(CLIENT_ID) |
| 78 | + .setClientSecret(CLIENT_SECRET) |
| 79 | + // Set the name of the callback function in the script referenced |
| 80 | + // above that should be invoked to complete the OAuth flow. |
| 81 | + .setCallbackFunction('authCallback') |
| 82 | + // Set the property store where authorized tokens should be persisted. |
| 83 | + .setPropertyStore(PropertiesService.getUserProperties()); |
| 84 | +} |
| 85 | + |
| 86 | +/** |
| 87 | + * Handles the OAuth callback. |
| 88 | + */ |
| 89 | +function authCallback(request) |
| 90 | +{ |
| 91 | + var service = getService(); |
| 92 | + var authorized = service.handleCallback(request); |
| 93 | + if (authorized) |
| 94 | + { |
| 95 | + PropertiesService.getUserProperties() |
| 96 | + .setProperty('QuickBooks.companyId', request.parameter.realmId); |
| 97 | + Logger.log("Success!"); |
| 98 | + return HtmlService.createHtmlOutput('Success!'); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + return HtmlService.createHtmlOutput('Denied'); |
| 103 | + } |
| 104 | +} |
0 commit comments