|
| 1 | +/* |
| 2 | + * This sample demonstrates how to configure the library for |
| 3 | + * the Intuit Quickbooks API. Instructions for obtaining your |
| 4 | + * Client ID and Client Secret can be found here: |
| 5 | + * https://developer.intuit.com/app/developer/qbo/docs/get-started |
| 6 | + */ |
| 7 | + |
| 8 | +var CLIENT_ID = ''; |
| 9 | +var CLIENT_SECRET = ''; |
| 10 | + |
| 11 | +/** |
| 12 | + * Log the redirect URI to be pasted in the Intuit Dev Center: |
| 13 | + * https://developer.intuit.com/v2/ui#/app/<YOURAPPID>/keys |
| 14 | + */ |
| 15 | +function logRedirectUri() { |
| 16 | + Logger.log(getService().getRedirectUri()); |
| 17 | +} |
| 18 | + |
| 19 | +/** |
| 20 | + * Authorizes and makes a request to the QuickBooks API. Assumes the use of a |
| 21 | + * sandbox company. |
| 22 | + */ |
| 23 | +function run() { |
| 24 | + var service = getService(); |
| 25 | + if (service.hasAccess()) { |
| 26 | + // Get the Company ID to be used in the request. |
| 27 | + var companyId = service.getStorage().getValue('QuickBooks.companyId'); |
| 28 | + // Get Quickbooks Company information to test. |
| 29 | + var url = 'https://sandbox-quickbooks.api.intuit.com/v3/company/' + |
| 30 | + companyId + '/companyinfo/' + companyId; |
| 31 | + var response = UrlFetchApp.fetch(url, { |
| 32 | + headers: { |
| 33 | + Authorization: 'Bearer ' + service.getAccessToken(), |
| 34 | + Accept: 'application/json' |
| 35 | + } |
| 36 | + }); |
| 37 | + var result = JSON.parse(response.getContentText()); |
| 38 | + Logger.log(JSON.stringify(result, null, 2)); |
| 39 | + } else { |
| 40 | + // If not authorized, get authorization URL. |
| 41 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 42 | + // View the Log to obtain the URL. |
| 43 | + Logger.log('Open the following URL and re-run the script: %s', |
| 44 | + authorizationUrl); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Reset the authorization state, so that it can be re-tested. |
| 50 | + */ |
| 51 | +function reset() { |
| 52 | + var service = getService(); |
| 53 | + service.reset(); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Configures the service. |
| 58 | + */ |
| 59 | +function getService() { |
| 60 | + return OAuth2.createService('Quickbooks') |
| 61 | + // Set the endpoint URLs. |
| 62 | + .setAuthorizationBaseUrl('https://appcenter.intuit.com/connect/oauth2') |
| 63 | + .setTokenUrl('https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer') |
| 64 | + // Set the client ID and secret. |
| 65 | + .setClientId(CLIENT_ID) |
| 66 | + .setClientSecret(CLIENT_SECRET) |
| 67 | + // Required, set to Accounting for this example, |
| 68 | + // see QB developer portal for additional options. |
| 69 | + .setScope('com.intuit.quickbooks.accounting') |
| 70 | + // Set the name of the callback function in the script referenced |
| 71 | + // above that should be invoked to complete the OAuth flow. |
| 72 | + .setCallbackFunction('authCallback') |
| 73 | + // Set the property store where authorized tokens should be persisted. |
| 74 | + .setPropertyStore(PropertiesService.getUserProperties()); |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Handles the OAuth callback. |
| 79 | + */ |
| 80 | +function authCallback(request) { |
| 81 | + var service = getService(); |
| 82 | + var authorized = service.handleCallback(request); |
| 83 | + if (authorized) { |
| 84 | + // Save the Company ID in the service's storage. |
| 85 | + service.getStorage().setValue('QuickBooks.companyId', |
| 86 | + request.parameter.realmId); |
| 87 | + return HtmlService.createHtmlOutput('Success!'); |
| 88 | + } else { |
| 89 | + return HtmlService.createHtmlOutput('Denied'); |
| 90 | + } |
| 91 | +} |
0 commit comments