Skip to content

Commit c921c9d

Browse files
author
Eric Koleda
committed
Add Xero samples. Fixes googleworkspace#236.
1 parent f4939f7 commit c921c9d

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

samples/Xero.gs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* This sample demonstrates how to configure the library for the Xero API.
3+
* Instructions on how to generate OAuth2 credentuals is available here:
4+
* https://developer.xero.com/documentation/oauth2/auth-flow
5+
*/
6+
7+
var CLIENT_ID = '...';
8+
var CLIENT_SECRET = '...';
9+
10+
/**
11+
* Authorizes and makes a request to the Xero API.
12+
*/
13+
function run() {
14+
var service = getService();
15+
if (service.hasAccess()) {
16+
// Retrieve the tenantId from storage.
17+
var tenantId = service.getStorage().getValue('tenantId');
18+
// Make a request to retrieve user information.
19+
var url = 'https://api.xero.com/api.xro/2.0/Organisations';
20+
var response = UrlFetchApp.fetch(url, {
21+
headers: {
22+
'Authorization': 'Bearer ' + service.getAccessToken(),
23+
'Xero-tenant-id': tenantId
24+
},
25+
});
26+
var result = JSON.parse(response.getContentText());
27+
Logger.log(JSON.stringify(result, null, 2));
28+
} else {
29+
var authorizationUrl = service.getAuthorizationUrl();
30+
Logger.log('Open the following URL and re-run the script: %s',
31+
authorizationUrl);
32+
}
33+
}
34+
35+
/**
36+
* Reset the authorization state, so that it can be re-tested.
37+
*/
38+
function reset() {
39+
getService().reset();
40+
}
41+
42+
/**
43+
* Configures the service.
44+
*/
45+
function getService() {
46+
return OAuth2.createService('Xero')
47+
// Set the endpoint URLs.
48+
.setAuthorizationBaseUrl(
49+
'https://login.xero.com/identity/connect/authorize')
50+
.setTokenUrl('https://identity.xero.com/connect/token')
51+
52+
// Set the client ID and secret.
53+
.setClientId(CLIENT_ID)
54+
.setClientSecret(CLIENT_SECRET)
55+
56+
// Set the name of the callback function that should be invoked to
57+
// complete the OAuth flow.
58+
.setCallbackFunction('authCallback')
59+
60+
// Set the property store where authorized tokens should be persisted.
61+
.setPropertyStore(PropertiesService.getScriptProperties())
62+
63+
// Set the scopes to request from the user. The scope "offline_access" is
64+
// required to refresh the token. The full list of scopes is available here:
65+
// https://developer.xero.com/documentation/oauth2/scopes
66+
.setScope('accounting.settings.read offline_access');
67+
};
68+
69+
/**
70+
* Handles the OAuth callback.
71+
*/
72+
function authCallback(request) {
73+
var service = getService();
74+
var authorized = service.handleCallback(request);
75+
if (authorized) {
76+
// Retrieve the connected tenants.
77+
var response = UrlFetchApp.fetch('https://api.xero.com/connections', {
78+
headers: {
79+
Authorization: 'Bearer ' + service.getAccessToken()
80+
},
81+
});
82+
var connections = JSON.parse(response.getContentText());
83+
// Store the first tenant ID in the service's storage. If you want to
84+
// support multiple tenants, store the full list and then let the user
85+
// select which one to operate against.
86+
service.getStorage().setValue('tenantId', connections[0].tenantId);
87+
return HtmlService.createHtmlOutput('Success!');
88+
} else {
89+
return HtmlService.createHtmlOutput('Denied.');
90+
}
91+
}
92+
93+
/**
94+
* Logs the redict URI to register in the Dropbox application settings.
95+
*/
96+
function logRedirectUri() {
97+
Logger.log(OAuth2.getRedirectUri());
98+
}

0 commit comments

Comments
 (0)