|
| 1 | +/** |
| 2 | + * This sample that demonstrates how to configured the library for the Xero API, |
| 3 | + * when using a private application. Although the Xero documentation calls it a |
| 4 | + * "2 legged" flow it is really a 1-legged flow. Public Xero applications use |
| 5 | + * a 3-legged flow not shown here. |
| 6 | + * @see {@link https://developer.xero.com/documentation/auth-and-limits/private-applications} |
| 7 | + */ |
| 8 | + |
| 9 | +var CONSUMER_KEY = '...'; |
| 10 | +// The private key must be in the PKCS#8 PEM format. |
| 11 | +var PRIVATE_KEY = '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Authorizes and makes a request to the Xero API. |
| 15 | + */ |
| 16 | +function run() { |
| 17 | + var service = getService(); |
| 18 | + var url = 'https://api.xero.com/api.xro/2.0/Organisations'; |
| 19 | + var response = service.fetch(url, { |
| 20 | + headers: { |
| 21 | + Accept: 'application/json' |
| 22 | + } |
| 23 | + }); |
| 24 | + var result = JSON.parse(response.getContentText()); |
| 25 | + Logger.log(JSON.stringify(result, null, 2)); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Reset the authorization state, so that it can be re-tested. |
| 30 | + */ |
| 31 | +function reset() { |
| 32 | + getService().reset(); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Configures the service. |
| 37 | + */ |
| 38 | +function getService() { |
| 39 | + return OAuth1.createService('Xero') |
| 40 | + // Set the consumer key and secret. |
| 41 | + .setConsumerKey(CONSUMER_KEY) |
| 42 | + .setConsumerSecret(PRIVATE_KEY) |
| 43 | + |
| 44 | + // Manually set the token to be the consumer key. |
| 45 | + .setAccessToken(CONSUMER_KEY) |
| 46 | + |
| 47 | + // Set the OAuth signature method. |
| 48 | + .setSignatureMethod('RSA-SHA1'); |
| 49 | +} |
0 commit comments