|
| 1 | +/** |
| 2 | + * Yandex Passport https://oauth.yandex.ru |
| 3 | + * |
| 4 | + */ |
| 5 | + |
| 6 | +var CLIENT_ID = '...'; |
| 7 | +var CLIENT_SECRET = '...'; |
| 8 | + |
| 9 | +/** |
| 10 | + * Authorizes and makes a request to the Yandex Passport API. |
| 11 | + */ |
| 12 | +function run() { |
| 13 | + var service = getService(); |
| 14 | + if (service.hasAccess()) { |
| 15 | + var url = 'https://login.yandex.ru/info'; |
| 16 | + var response = UrlFetchApp.fetch(url, { |
| 17 | + headers: { |
| 18 | + Authorization: 'Bearer ' + service.getAccessToken() |
| 19 | + } |
| 20 | + }); |
| 21 | + var result = JSON.parse(response.getContentText()); |
| 22 | + Logger.log(JSON.stringify(result, null, 2)); |
| 23 | + } else { |
| 24 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 25 | + Logger.log('Open the following URL and re-run the script: %s', |
| 26 | + authorizationUrl); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Reset the authorization state, so that it can be re-tested. |
| 32 | + */ |
| 33 | +function reset() { |
| 34 | + var service = getService(); |
| 35 | + service.reset(); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Configures the service. |
| 40 | + */ |
| 41 | +function getService() { |
| 42 | + return OAuth2.createService('Yandex') |
| 43 | + // Set the endpoint URLs. |
| 44 | + .setAuthorizationBaseUrl('https://oauth.yandex.ru/authorize') |
| 45 | + .setTokenUrl('https://oauth.yandex.ru/token') |
| 46 | + |
| 47 | + // Set the client ID and secret. |
| 48 | + .setClientId(CLIENT_ID) |
| 49 | + .setClientSecret(CLIENT_SECRET) |
| 50 | + |
| 51 | + // Set the name of the callback function that should be invoked to complete |
| 52 | + // the OAuth flow. |
| 53 | + .setCallbackFunction('authCallback') |
| 54 | + |
| 55 | + // Set the property store where authorized tokens should be persisted. |
| 56 | + .setPropertyStore(PropertiesService.getUserProperties()) |
| 57 | + |
| 58 | + // Set the scope and additional specific parameters if its are supported |
| 59 | + //.setScope() // There is no need to pass a scope for the passport getting. But you need to provide the scope for specific API |
| 60 | + .setParam('access_type', 'offline') |
| 61 | + .setParam('approval_prompt', 'force'); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Handles the OAuth callback. |
| 66 | + */ |
| 67 | +function authCallback(request) { |
| 68 | + var service = getService(); |
| 69 | + var authorized = service.handleCallback(request); |
| 70 | + if (authorized) { |
| 71 | + return HtmlService.createHtmlOutput('Success!'); |
| 72 | + } else { |
| 73 | + return HtmlService.createHtmlOutput('Denied'); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Logs the redict URI to register in the Yandex oAuth Page https://oauth.yandex.ru/client/new. |
| 79 | + */ |
| 80 | +function logRedirectUri() { |
| 81 | + var service = getService(); |
| 82 | + Logger.log(service.getRedirectUri()); |
| 83 | +} |
0 commit comments