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