|
| 1 | +var CONSUMER_KEY = '...'; |
| 2 | +var CONSUMER_SECRET = '...'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Authorizes and makes a request to the Xing API. |
| 6 | + */ |
| 7 | +function run() { |
| 8 | + var service = getService(); |
| 9 | + if (service.hasAccess()) { |
| 10 | + var url = 'https://api.xing.com/v1/users/me'; |
| 11 | + var response = service.fetch(url); |
| 12 | + var result = JSON.parse(response.getContentText()); |
| 13 | + Logger.log(JSON.stringify(result, null, 2)); |
| 14 | + } else { |
| 15 | + var authorizationUrl = service.authorize(); |
| 16 | + Logger.log('Open the following URL and re-run the script: %s', |
| 17 | + authorizationUrl); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Reset the authorization state, so that it can be re-tested. |
| 23 | + */ |
| 24 | +function reset() { |
| 25 | + var service = getService(); |
| 26 | + service.reset(); |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Configures the service. |
| 31 | + */ |
| 32 | +function getService() { |
| 33 | + return OAuth1.createService('Xing') |
| 34 | + // Set the endpoint URLs. |
| 35 | + .setRequestTokenUrl('https://api.xing.com/v1/request_token') |
| 36 | + .setAuthorizationUrl('https://api.xing.com/v1/authorize') |
| 37 | + .setAccessTokenUrl('https://api.xing.com/v1/access_token') |
| 38 | + |
| 39 | + // Set the consumer key and secret. |
| 40 | + .setConsumerKey(CONSUMER_KEY) |
| 41 | + .setConsumerSecret(CONSUMER_SECRET) |
| 42 | + |
| 43 | + // Set the name of the callback function in the script referenced |
| 44 | + // above that should be invoked to complete the OAuth flow. |
| 45 | + .setCallbackFunction('authCallback') |
| 46 | + |
| 47 | + // Set the property store where authorized tokens should be persisted. |
| 48 | + .setPropertyStore(PropertiesService.getUserProperties()); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Handles the OAuth2 callback. |
| 53 | + */ |
| 54 | +function authCallback(request) { |
| 55 | + var service = getService(); |
| 56 | + var authorized = service.handleCallback(request); |
| 57 | + if (authorized) { |
| 58 | + return HtmlService.createHtmlOutput('Success!'); |
| 59 | + } else { |
| 60 | + return HtmlService.createHtmlOutput('Denied'); |
| 61 | + } |
| 62 | +} |
0 commit comments