-
Notifications
You must be signed in to change notification settings - Fork 548
/
vk.js
91 lines (72 loc) · 1.72 KB
/
vk.js
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Vkontakte (vk.com)
(function(hello) {
hello.init({
vk: {
name: 'Vk',
// See https://vk.com/dev/oauth_dialog
oauth: {
version: 2,
auth: 'https://oauth.vk.com/authorize',
grant: 'https://oauth.vk.com/access_token'
},
// Authorization scopes
// See https://vk.com/dev/permissions
scope: {
email: 'email',
friends: 'friends',
photos: 'photos',
videos: 'video',
share: 'share',
offline_access: 'offline'
},
// Refresh the access_token
refresh: true,
login: function(p) {
p.qs.display = window.navigator &&
window.navigator.userAgent &&
/ipad|phone|phone|android/.test(window.navigator.userAgent.toLowerCase()) ? 'mobile' : 'popup';
},
// API Base URL
base: 'https://api.vk.com/method/',
// Map GET requests
get: {
me: function(p, callback) {
p.query.fields = 'id,first_name,last_name,photo_max';
callback('users.get');
}
},
wrap: {
me: function(res, headers, req) {
formatError(res);
return formatUser(res, req);
}
},
// No XHR
xhr: false,
// All requests should be JSONP as of missing CORS headers in https://api.vk.com/method/*
jsonp: true,
// No form
form: false
}
});
function formatUser(o, req) {
if (o !== null && 'response' in o && o.response !== null && o.response.length) {
o = o.response[0];
o.id = o.uid;
o.thumbnail = o.picture = o.photo_max;
o.name = o.first_name + ' ' + o.last_name;
if (req.authResponse && req.authResponse.email !== null)
o.email = req.authResponse.email;
}
return o;
}
function formatError(o) {
if (o.error) {
var e = o.error;
o.error = {
code: e.error_code,
message: e.error_msg
};
}
}
})(hello);