forked from 57uff3r/nodejs-vksdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvksdk.js
350 lines (304 loc) · 10.4 KB
/
vksdk.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
*
* SDK for vk.com API
*
* @author [email protected]
* @see https://github.com/57uff3r/nodejs-vksdk
* @see http://57uff3r.ru
*/
var util = require('util');
EventEmitter = require('events').EventEmitter,
crypto = require('crypto'),
http = require('http'),
https = require('https');
/**
* Create new SDK object
* @param {object} _options
* - mode - auth mode (oauth or sig)
* - appID -
* @returns {undefined}
*/
var VK = function(_options) {
var self = this;
// appID, appSecret, mode (sig, oauth), [version], [language]
self.options = _options;
// Default settings
self.default = {
version: '3.0',
language: 'ru'
};
/**
* APi method request
* @param {string} _method APi method name
* @param {mixed} _requestParams object or null (or undef), API method params
* @param {mixed} _response string, function or null (or undef), callback function or custom event name
* @param {string} _responseType define type of response callback or event
* @returns {undefined}
*/
self.request = function(_method, _requestParams, _response) {
var responseType = 'event';
if ( typeof(_response) === 'function') {
responseType = 'callback';
}
if (self.options.mode === 'sig') {
self._sigRequest(_method, _requestParams, _response, responseType);
} else if (self.options.mode === 'oauth') {
self._oauthRequest(_method, _requestParams, _response, responseType);
} else {
throw 'nodejs-vk-sdk: you have to specify sdk work mode (sig or oauth) before requests.';
}
};
/**
* Change SDK request mode
* @param {string} _mode sig or outh
* @returns {undefined}
*/
self.changeMode = function(_mode) {
self.mode = _mode;
};
/**
* Update oauth token
* @param {mixed} _param
* empty - for server-side api requests
* { code : string } - obtain token with code
* { token : string } - setup token manually
* @returns {undefined}
*/
self.setToken = function(_param) {
if (typeof(_param) === 'object') {
if (_param.token) {
self.token = _param.token;
} else if (_param.code) {
self._setUpTokenByCode(_param.code);
}
} else {
self._setUpAppServerToken();
}
};
/**
* Get current token
* @returns {string}
*/
self.getToken = function() {
return self.token;
};
/**
* Get token by login and password
*
* @param {string} _username vk user username
* @param {string} _password vk user password
* @returns {undefined}
*
* @see https://vk.com/dev/auth_direct
*/
self.acquireToken = function(_username, _password) {
var options = {
host: 'oauth.vk.com',
port: 443,
path: '/access_token?grant_type=password&client_id=' + self.options.appID +
'&client_secret=' + self.options.appSecret +
'&grant_type=password&scope=notify,friends,photos,audio,video,docs,messages,notifications,offline,wall' +
'&username=' + _username +
'&password=' + _password
};
https.get(options, function(res) {
var apiResponse = new String();
res.setEncoding('utf8');
res.on('data', function(chunk) {
apiResponse += chunk;
});
res.on('end', function() {
var o = JSON.parse(apiResponse);
if (!o.access_token) {
self.emit('acquireTokenNotReady', o);
} else {
self.token = o.access_token;
self.emit('acquireTokenReady');
}
});
});
};
/**
* Obtain token with code
* @param {string} _code
* @returns {undefined}
*/
self._setUpTokenByCode = function(_code) {
var options = {
host: 'oauth.vk.com',
port: 443,
path: '/access_token?client_id=' + self.options.appID +
'&client_secret=' + self.options.appSecret +
'&code=' + _code
};
https.get(options, function(res) {
var apiResponse = new String();
res.setEncoding('utf8');
res.on('data', function(chunk) {
apiResponse += chunk;
});
res.on('end', function() {
var o = JSON.parse(apiResponse);
if (!o.access_token) { self.emit('tokenByCodeNotReady', o);
} else {
self.token = o.access_token;
self.emit('tokenByCodeReady');
}
});
});
};
/**
* Obtain server-side token
* @returns {undefined}
*/
self._setUpAppServerToken = function() {
var options = {
host: 'api.vk.com',
port: 443,
path: '/oauth/access_token?client_id=' + self.options.appID +
'&client_secret=' + self.options.appSecret +
'&grant_type=client_credentials'
};
https.get(options, function(res) {
var apiResponse = new String();
res.setEncoding('utf8');
res.on('data', function(chunk) {
apiResponse += chunk;
});
res.on('end', function() {
var o = JSON.parse(apiResponse);
if (o.error) { self.emit('appServerTokenNotReady', o);
} else {
self.token = o.access_token;
self.emit('appServerTokenReady');
}
});
});
};
/**
* Outh api request
* @param {string} _method
* @param {mixed} _params
* @param {mixed} _response
* @param {string} _responseType
* @returns {undefined}
*/
self._oauthRequest = function(_method, _params, _response, _responseType) {
var options = {
host: 'api.vk.com',
port: 443,
path: '/method/' + _method + '?' +
'access_token=' + self.token
};
_params['v'] = _params['v'] || self.options.version || self.default.version;
_params['lang'] = _params['lang'] || self.options.language || self.default.language;
for(var key in _params) {
if( key === "message" ) {
options.path += ('&' + key + '=' + encodeURIComponent(_params[key]));
} else {
options.path += ('&' + key + '=' + _params[key]);
}
}
https.get(options, function(res) {
var apiResponse = new String();
res.setEncoding('utf8');
res.on('data', function(chunk) {
apiResponse += chunk;
});
res.on('end', function() {
var o = JSON.parse(apiResponse);
if (_responseType === 'callback' && typeof _response === 'function') {
_response(o);
} else {
if (!_response) self.emit('done:' + _method, o);
else self.emit(_response, o);
}
});
});
};
/**
* Request API method with signature
* @param {string} _method
* @param {mixed} _params
* @param {mixed} _response
* @param {string} _responseType
* @returns {undefined}
*/
self._sigRequest = function(_method, _params, _response, _responseType) {
var params = (!!_params ? _params : {});
params.api_id = self.options.appID;
params.v = ('v' in params) ? params['v'] : self.options.version || self.default.version;
params.lang = ('lang' in params) ? params['lang'] : self.options.language || self.default.language,
params.method = _method;
params.timestamp = new Date().getTime();
params.format = 'json';
params.random = Math.floor(Math.random() * 9999);
params = this._sortObjectByKey(params);
var sig = '';
for(var key in params) {
sig = sig + key + '=' + params[key];
}
sig = sig + self.options.appSecret;
params.sig = crypto.createHash('md5').update(sig, 'utf8').digest('hex');
var requestArray = new Array();
for(var key in params) {
if( key === "message" ) {
requestArray.push(key + '=' + encodeURIComponent(params[key]) );
} else {
requestArray.push(key + '=' + (params[key]) );
}
}
var requestString = this._implode('&', requestArray);
var options = {
host: 'api.vk.com',
port: 80,
path: '/api.php?' + requestString
};
http.get(options, function(res) {
var apiResponse = new String();
res.setEncoding('utf8');
res.on('data', function(chunk) {
apiResponse += chunk;
});
res.on('end', function() {
var o = JSON.parse(apiResponse);
if (_responseType === 'callback' && typeof _response === 'function') {
_response(o);
} else {
if (!_response) self.emit('done:' + _method, o);
else self.emit(_response, o);
}
});
});
};
/**
* Implode array to string
* @param {string} glue
* @param {array} pieces
* @returns {@exp;pieces@call;join|@exp;@exp;pieces@call;joinpieces|VK.implode.pieces}
*/
self._implode = function implode( glue, pieces ) {
return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );
};
/**
* Sort object properties by name
* @param {object} o
* @returns {object}
*/
self._sortObjectByKey = function (o) {
var sorted = {},
key, a = [];
for (key in o) {
if (o.hasOwnProperty(key)) {
a.push(key);
}
}
a.sort();
for (var key = 0; key < a.length; key++) {
sorted[a[key]] = o[a[key]];
}
return sorted;
};
};
util.inherits(VK, EventEmitter);
module.exports = VK;