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