|
| 1 | +/** |
| 2 | + * Demonstrates how to authorize access to the Jira API using the authorization |
| 3 | + * code grants (3LO) for apps flow. |
| 4 | + * @see {@link https://developer.atlassian.com/cloud/jira/platform/oauth-2-authorization-code-grants-3lo-for-apps/} |
| 5 | + */ |
| 6 | + |
| 7 | +var CLIENT_ID = '...'; |
| 8 | +var CLIENT_SECRET = '...'; |
| 9 | + |
| 10 | +// The key to use when storing the cloudid. |
| 11 | +var CLOUDID_KEY = 'cloudid'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Authorizes and makes a request to the UltraCart API. |
| 15 | + */ |
| 16 | +function run() { |
| 17 | + var service = getService(); |
| 18 | + if (service.hasAccess()) { |
| 19 | + var cloudid = getCloudId(service); |
| 20 | + var url = 'https://api.atlassian.com/ex/jira/' + cloudid + |
| 21 | + '/rest/api/3/myself'; |
| 22 | + var response = UrlFetchApp.fetch(url, { |
| 23 | + headers: { |
| 24 | + Accept: 'application/json', |
| 25 | + Authorization: 'Bearer ' + service.getAccessToken() |
| 26 | + } |
| 27 | + }); |
| 28 | + var result = JSON.parse(response.getContentText()); |
| 29 | + Logger.log(JSON.stringify(result, null, 2)); |
| 30 | + } else { |
| 31 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 32 | + Logger.log('Open the following URL and re-run the script: %s', |
| 33 | + authorizationUrl); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Gets the cloudid of the Jira site to operate against. This implementation |
| 39 | + * selects the first authorized site, but in a real application you'd probably |
| 40 | + * want to let the user select which site to operate against. After the first |
| 41 | + * run the cloudid is saved into the service's storage layer for easy retrieval. |
| 42 | + * @param {OAuth.Service_} service The authorized service. |
| 43 | + * @returns {string} The cloudid of the site to operate on. |
| 44 | + */ |
| 45 | +function getCloudId(service) { |
| 46 | + var cloudid = service.getStorage().getValue(CLOUDID_KEY); |
| 47 | + if (cloudid) return cloudid; |
| 48 | + // Get the cloudid of the first site the user has access to. |
| 49 | + var url = 'https://api.atlassian.com/oauth/token/accessible-resources'; |
| 50 | + var response = UrlFetchApp.fetch(url, { |
| 51 | + headers: { |
| 52 | + Accept: 'application/json', |
| 53 | + Authorization: 'Bearer ' + service.getAccessToken() |
| 54 | + } |
| 55 | + }); |
| 56 | + var result = JSON.parse(response.getContentText()); |
| 57 | + cloudid = result[0].id; |
| 58 | + service.getStorage().setValue(CLOUDID_KEY, cloudid); |
| 59 | + return cloudid; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Reset the authorization state, so that it can be re-tested. |
| 64 | + */ |
| 65 | +function reset() { |
| 66 | + getService().reset(); |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Configures the service. |
| 71 | + */ |
| 72 | +function getService() { |
| 73 | + return OAuth2.createService('Jira') |
| 74 | + // Set the endpoint URLs. |
| 75 | + .setAuthorizationBaseUrl('https://accounts.atlassian.com/authorize') |
| 76 | + .setTokenUrl('https://accounts.atlassian.com/oauth/token') |
| 77 | + |
| 78 | + // Set the client ID and secret. |
| 79 | + .setClientId(CLIENT_ID) |
| 80 | + .setClientSecret(CLIENT_SECRET) |
| 81 | + |
| 82 | + // Set the name of the callback function that should be invoked to |
| 83 | + // complete the OAuth flow. |
| 84 | + .setCallbackFunction('authCallback') |
| 85 | + |
| 86 | + // Set the property store where authorized tokens should be persisted. |
| 87 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 88 | + |
| 89 | + // Set the scope and other paramaeters required by Atlassian. |
| 90 | + .setScope('read:jira-user') |
| 91 | + .setParam('audience', 'api.atlassian.com') |
| 92 | + .setParam('prompt', 'consent'); |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Handles the OAuth callback. |
| 97 | + */ |
| 98 | +function authCallback(request) { |
| 99 | + var service = getService(); |
| 100 | + var authorized = service.handleCallback(request); |
| 101 | + if (authorized) { |
| 102 | + return HtmlService.createHtmlOutput('Success!'); |
| 103 | + } else { |
| 104 | + return HtmlService.createHtmlOutput('Denied'); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Logs the redict URI to register. |
| 110 | + */ |
| 111 | +function logRedirectUri() { |
| 112 | + Logger.log(getService().getRedirectUri()); |
| 113 | +} |
0 commit comments