Skip to content
This repository was archived by the owner on Nov 18, 2025. It is now read-only.

Commit 3ad32cc

Browse files
author
Eric Koleda
authored
Merge pull request #42 from gsuitedevs/jira
Add Jira sample and better error handling
2 parents 3dd9cd7 + 8b19fe1 commit 3ad32cc

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

samples/Jira.gs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* This sample demonstrates how to configure the library for the Jira REST API.
3+
* @see {@link https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-oauth-authentication/}
4+
*/
5+
6+
var SITE = 'https://something.atlassian.net';
7+
var CONSUMER_KEY = '...';
8+
// The private key must be in the PKCS#8 PEM format.
9+
var PRIVATE_KEY = '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n';
10+
11+
/**
12+
* Authorizes and makes a request to the Xero API.
13+
*/
14+
function run() {
15+
var service = getService();
16+
if (service.hasAccess()) {
17+
var url = SITE + '/rest/api/3/myself';
18+
var response = service.fetch(url, {
19+
headers: {
20+
Accept: 'application/json'
21+
}
22+
});
23+
var result = JSON.parse(response.getContentText());
24+
Logger.log(JSON.stringify(result, null, 2));
25+
} else {
26+
var authorizationUrl = service.authorize();
27+
Logger.log('Open the following URL and re-run the script: %s',
28+
authorizationUrl);
29+
}
30+
}
31+
32+
/**
33+
* Reset the authorization state, so that it can be re-tested.
34+
*/
35+
function reset() {
36+
getService().reset();
37+
}
38+
39+
/**
40+
* Configures the service.
41+
*/
42+
function getService() {
43+
return OAuth1.createService('Jira')
44+
// Set the endpoint URLs.
45+
.setRequestTokenUrl(SITE + '/plugins/servlet/oauth/request-token')
46+
.setAuthorizationUrl(SITE + '/plugins/servlet/oauth/authorize')
47+
.setAccessTokenUrl(SITE + '/plugins/servlet/oauth/access-token')
48+
49+
// Set the consumer key and secret.
50+
.setConsumerKey(CONSUMER_KEY)
51+
.setConsumerSecret(PRIVATE_KEY)
52+
53+
// Set the OAuth signature method.
54+
.setSignatureMethod('RSA-SHA1')
55+
56+
// Set the method to POST, as required by Atlassian.
57+
.setMethod('post')
58+
59+
// Set the name of the callback function in the script referenced
60+
// above that should be invoked to complete the OAuth flow.
61+
.setCallbackFunction('authCallback')
62+
63+
// Set the property store where authorized tokens should be persisted.
64+
.setPropertyStore(PropertiesService.getUserProperties());
65+
}
66+
67+
/**
68+
* Handles the OAuth callback.
69+
*/
70+
function authCallback(request) {
71+
var service = getService();
72+
var authorized = service.handleCallback(request);
73+
if (authorized) {
74+
return HtmlService.createHtmlOutput('Success!');
75+
} else {
76+
return HtmlService.createHtmlOutput('Denied');
77+
}
78+
}

samples/XeroPrivate.gs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
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
2+
* This sample demonstrates how to configure the library for the Xero API when
3+
* using a private application. Although the Xero documentation calls it a
44
* "2 legged" flow it is really a 1-legged flow. Public Xero applications use
55
* a 3-legged flow not shown here.
66
* @see {@link https://developer.xero.com/documentation/auth-and-limits/private-applications}

src/Service.gs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,10 @@ Service_.prototype.parseToken_ = function(content) {
455455
result[decodeURIComponent(parts[0])] = decodeURIComponent(parts[1]);
456456
return result;
457457
}, {});
458+
// Verify that the response contains a token.
459+
if (!token.oauth_token) {
460+
throw 'Error parsing token: key "oauth_token" not found';
461+
}
458462
// Set fields that the signing library expects.
459463
token.public = token.oauth_token;
460464
token.secret = token.oauth_token_secret;

0 commit comments

Comments
 (0)