|
| 1 | +/** |
| 2 | + * |
| 3 | + * VK's Auth flow https://vk.com/dev/authcode_flow_user |
| 4 | + * Scopes list https://vk.com/dev/permissions |
| 5 | + * Be careful. Implicit Flow's Methods are not used on the server side. They will always return "error_code": 15, "error_msg": "Access denied: no access to call this method" |
| 6 | + */ |
| 7 | + |
| 8 | +var CLIENT_ID = '...'; |
| 9 | +var CLIENT_SECRET = '...'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Authorizes and makes a request to the VK API. |
| 13 | + */ |
| 14 | +function run() { |
| 15 | + var service = getService(); |
| 16 | + if (service.hasAccess()) { |
| 17 | + // GET requests require access_token parameter |
| 18 | + var url = 'https://api.vk.com/method/groups.get?access_token=' + service.getAccessToken(); |
| 19 | + var response = UrlFetchApp.fetch(url); |
| 20 | + var result = JSON.parse(response.getContentText()); |
| 21 | + Logger.log(JSON.stringify(result, null, 2)); |
| 22 | + } else { |
| 23 | + var authorizationUrl = service.getAuthorizationUrl(); |
| 24 | + Logger.log('Open the following URL and re-run the script: %s', |
| 25 | + authorizationUrl); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Reset the authorization state, so that it can be re-tested. |
| 31 | + */ |
| 32 | +function reset() { |
| 33 | + var service = getService(); |
| 34 | + service.reset(); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Configures the service. |
| 39 | + */ |
| 40 | +function getService() { |
| 41 | + return OAuth2.createService('VK') |
| 42 | + // Set the endpoint URLs. |
| 43 | + .setAuthorizationBaseUrl('https://oauth.vk.com/authorize') |
| 44 | + .setTokenUrl('https://oauth.vk.com/access_token') |
| 45 | + |
| 46 | + // Set the client ID and secret. |
| 47 | + .setClientId(CLIENT_ID) |
| 48 | + .setClientSecret(CLIENT_SECRET) |
| 49 | + |
| 50 | + // Set the name of the callback function that should be invoked to complete |
| 51 | + // 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 scope and additional specific parameters if its are supported |
| 58 | + .setScope('groups,offline') |
| 59 | + .setParam('approval_prompt', 'force'); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Handles the OAuth callback. |
| 64 | + */ |
| 65 | +function authCallback(request) { |
| 66 | + var service = getService(); |
| 67 | + var authorized = service.handleCallback(request); |
| 68 | + if (authorized) { |
| 69 | + return HtmlService.createHtmlOutput('Success!'); |
| 70 | + } else { |
| 71 | + return HtmlService.createHtmlOutput('Denied'); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Logs the redict URI to register in the VK Aps Page https://vk.com/apps?act=manage. |
| 77 | + */ |
| 78 | +function logRedirectUri() { |
| 79 | + var service = getService(); |
| 80 | + Logger.log(service.getRedirectUri()); |
| 81 | +} |
0 commit comments