|
| 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 bug in the Goodreads API's OAuth implementation |
| 5 | +* (https://goo.gl/cRfdph). |
| 6 | + */ |
| 7 | + |
| 8 | +var CONSUMER_KEY = '...'; |
| 9 | +var CONSUMER_SECRET = '...'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Authorizes and makes a request to the Twitter API. |
| 13 | + */ |
| 14 | +function run() { |
| 15 | + var service = getService(); |
| 16 | + if (service.hasAccess()) { |
| 17 | + var url = 'https://www.goodreads.com/api/auth_user'; |
| 18 | + var response = service.fetch(url); |
| 19 | + var result = response.getContentText(); |
| 20 | + Logger.log(result); |
| 21 | + } else { |
| 22 | + var authorizationUrl = service.authorize(); |
| 23 | + Logger.log('Open the following URL and re-run the script: %s', |
| 24 | + authorizationUrl); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Reset the authorization state, so that it can be re-tested. |
| 30 | + */ |
| 31 | +function reset() { |
| 32 | + var service = getService(); |
| 33 | + service.reset(); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Configures the service. |
| 38 | + */ |
| 39 | +function getService() { |
| 40 | + var service = OAuth1.createService('Goodreads') |
| 41 | + // Set the endpoint URLs. |
| 42 | + .setAccessTokenUrl('https://www.goodreads.com/oauth/access_token') |
| 43 | + .setRequestTokenUrl('https://www.goodreads.com/oauth/request_token') |
| 44 | + .setAuthorizationUrl('https://www.goodreads.com/oauth/authorize') |
| 45 | + |
| 46 | + // Set the consumer key and secret. |
| 47 | + .setConsumerKey(CONSUMER_KEY) |
| 48 | + .setConsumerSecret(CONSUMER_SECRET) |
| 49 | + |
| 50 | + // Set the name of the callback function in the script referenced |
| 51 | + // above that should be invoked to complete the OAuth flow. |
| 52 | + .setCallbackFunction('authCallback') |
| 53 | + |
| 54 | + // Set the property store where authorized tokens should be persisted. |
| 55 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 56 | + |
| 57 | + // Set the OAuth version (default: 1.0a) |
| 58 | + .setOAuthVersion('1.0'); |
| 59 | + |
| 60 | + // Override the callback URL method to use the web app URL instead. |
| 61 | + service.getCallbackUrl = function() { |
| 62 | + return ScriptApp.getService().getUrl(); |
| 63 | + }; |
| 64 | + |
| 65 | + return service; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Handles GET requests to the web app. |
| 70 | + */ |
| 71 | +function doGet(request) { |
| 72 | + // Determine if the request is part of an OAuth callback. |
| 73 | + if (request.parameter.oauth_token) { |
| 74 | + var service = getService(); |
| 75 | + var authorized = service.handleCallback(request); |
| 76 | + if (authorized) { |
| 77 | + return HtmlService.createHtmlOutput('Success!'); |
| 78 | + } else { |
| 79 | + return HtmlService.createHtmlOutput('Denied'); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments