|
| 1 | +/* |
| 2 | + * This script must be published as a web app (Publish > Deploy as web app) in |
| 3 | + * order to function. The web app URL is used instead of the normal callback |
| 4 | + * URL to work around a limitation in the Etsy API's OAuth implementation |
| 5 | + * (callback URLs are limited to 255 characters). |
| 6 | + */ |
| 7 | + |
| 8 | +var CONSUMER_KEY = '...'; |
| 9 | +var CONSUMER_SECRET = '...'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Authorizes and makes a request to the Etsy API. |
| 13 | + */ |
| 14 | +function run() { |
| 15 | + var service = getService(); |
| 16 | + if (service.hasAccess()) { |
| 17 | + var url = 'https://openapi.etsy.com/v2/users/__SELF__/profile'; |
| 18 | + var response = service.fetch(url); |
| 19 | + var result = JSON.parse(response.getContentText()); |
| 20 | + Logger.log(JSON.stringify(result, null, 2)); |
| 21 | + } else { |
| 22 | + service.authorize(); |
| 23 | + // Retrieve the authorization URL from the "login_url" field of the request |
| 24 | + // token. |
| 25 | + var authorizationUrl = service.getToken_().login_url; |
| 26 | + Logger.log('Open the following URL and re-run the script: %s', |
| 27 | + authorizationUrl); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Reset the authorization state, so that it can be re-tested. |
| 33 | + */ |
| 34 | +function reset() { |
| 35 | + var service = getService(); |
| 36 | + service.reset(); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Configures the service. |
| 41 | + */ |
| 42 | +function getService() { |
| 43 | + var service = OAuth1.createService('Etsy') |
| 44 | + // Set the endpoint URLs. |
| 45 | + // Pass the desired scopes in the request token URL. |
| 46 | + .setRequestTokenUrl('https://openapi.etsy.com/v2/oauth/request_token?scope=profile_r') |
| 47 | + // The authorization URL isn't used, since Etsy sends it back in the |
| 48 | + // "login_url" field of the request token. |
| 49 | + .setAuthorizationUrl('N/A') |
| 50 | + .setAccessTokenUrl('https://openapi.etsy.com/v2/oauth/access_token') |
| 51 | + |
| 52 | + // Set the consumer key and secret. |
| 53 | + .setConsumerKey(CONSUMER_KEY) |
| 54 | + .setConsumerSecret(CONSUMER_SECRET) |
| 55 | + |
| 56 | + // Set the name of the callback function in the script referenced |
| 57 | + // above that should be invoked to complete the OAuth flow. |
| 58 | + .setCallbackFunction('authCallback') |
| 59 | + |
| 60 | + // Set the property store where authorized tokens should be persisted. |
| 61 | + .setPropertyStore(PropertiesService.getUserProperties()); |
| 62 | + |
| 63 | + // Override the callback URL method to use the web app URL instead. |
| 64 | + service.getCallbackUrl = function() { |
| 65 | + return ScriptApp.getService().getUrl(); |
| 66 | + }; |
| 67 | + |
| 68 | + return service; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Handles GET requests to the web app. |
| 73 | + */ |
| 74 | +function doGet(request) { |
| 75 | + // Determine if the request is part of an OAuth callback. |
| 76 | + if (request.parameter.oauth_token) { |
| 77 | + var service = getService(); |
| 78 | + var authorized = service.handleCallback(request); |
| 79 | + if (authorized) { |
| 80 | + return HtmlService.createHtmlOutput('Success!'); |
| 81 | + } else { |
| 82 | + return HtmlService.createHtmlOutput('Denied'); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments