|
| 1 | +var CLIENT_ID = '...'; |
| 2 | +var CLIENT_SECRET = '...'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Authorizes and makes a request to the LinkedIn API. |
| 6 | + */ |
| 7 | +function run() { |
| 8 | + var service = getService(); |
| 9 | + if (service.hasAccess()) { |
| 10 | + var url = 'https://api.linkedin.com/v1/people/~?format=json'; |
| 11 | + var response = UrlFetchApp.fetch(url, { |
| 12 | + headers: { |
| 13 | + 'Authorization': 'Bearer ' + service.getAccessToken() |
| 14 | + } |
| 15 | + }); |
| 16 | + var result = JSON.parse(response.getContentText()); |
| 17 | + Logger.log(JSON.stringify(result, null, 2)); |
| 18 | + } else { |
| 19 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 20 | + Logger.log('Open the following URL and re-run the script: %s', |
| 21 | + authorizationUrl); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Reset the authorization state, so that it can be re-tested. |
| 27 | + */ |
| 28 | +function reset() { |
| 29 | + var service = getService(); |
| 30 | + service.reset(); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Configures the service. |
| 35 | + */ |
| 36 | +function getService() { |
| 37 | + return OAuth2.createService('LinkedIn') |
| 38 | + // Set the endpoint URLs. |
| 39 | + .setAuthorizationBaseUrl('https://www.linkedin.com/uas/oauth2/authorization') |
| 40 | + .setTokenUrl('https://www.linkedin.com/uas/oauth2/accessToken') |
| 41 | + |
| 42 | + // Set the client ID and secret. |
| 43 | + .setClientId(CLIENT_ID) |
| 44 | + .setClientSecret(CLIENT_SECRET) |
| 45 | + |
| 46 | + // Set the name of the callback function that should be invoked to complete |
| 47 | + // the OAuth flow. |
| 48 | + .setCallbackFunction('authCallback') |
| 49 | + |
| 50 | + // Set the property store where authorized tokens should be persisted. |
| 51 | + .setPropertyStore(PropertiesService.getUserProperties()); |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Handles the OAuth callback. |
| 56 | + */ |
| 57 | +function authCallback(request) { |
| 58 | + var service = getService(); |
| 59 | + var authorized = service.handleCallback(request); |
| 60 | + if (authorized) { |
| 61 | + return HtmlService.createHtmlOutput('Success!'); |
| 62 | + } else { |
| 63 | + return HtmlService.createHtmlOutput('Denied'); |
| 64 | + } |
| 65 | +} |
0 commit comments