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