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