Small SDK for vk.com API.
npm install vksdk
var VK = require('vksdk');
You can use two ways of performing API requests:
- sig (with signature and http)
- token (oauth or direct token request)
This SDK provides both ways:
var vk = new VK({
'appID' : 2807970,
'appSecret' : 'L1ZKpgQPalJdumI6vFK',
'mode' : 'oauth'
});
var vk = new VK({
'appID' : 2807970,
'appSecret' : 'L1ZKpgQPalJdumI6vFK',
'mode' : 'sig'
});
You also can change request mode 'on-fly':
vk.changeMode('oauth');
or
vk.changeMode('sig');
By default used API version 3.0 and Russian language.
You may setup latest version of vk.com API and change language.
var vk = new VK({
'appID' : 2807970,
'appSecret' : 'L1ZKpgQPalJdumI6vFK',
'mode' : 'oauth',
'version' : '5.26',
'language' : 'en'
});
You need just your appID and appSecret.
You need token to perform api requests.
SDK can automaticly provide tokens for server-side applications. With server-side token you can perform only limited set of api methods like secure.getAppBalance or secure.sendNotification.
SDK has two events for server-side token requests:
- appServerTokenReady - token is ready
- appServerTokenNotReady — something was wrong
vk.setToken();
vk.on('appServerTokenReady', function() {
vk.request('secure.getAppBalance');
// etc
});
vk.on('appServerTokenNotReady', function(_error) {
// error handler
});
Second way — get token for client API requests with special code from your fron-end.
vk.setToken({ code : '0819c207b9933a' });
vk.on('tokenByCodeReady', function() {
vk.request('getProfiles', {'uids' : '29894'});
// etc...
});
vk.on('tokenByCodeNotReady', function(_error) {
// error handler
});
Third way — get token directly from your application in customers browser.
vk.setToken( { token :'f1eebc4311e775b128183993ee16302ac036a67af30424238d1oo14d35dfa61896f172ee630b7034a' });
vk.request('getProfiles', {'uids' : '29894'});
vk.on('done:getProfiles', function(_o) {
console.log(_o);
});
Fourth way - get token using customers vk.com login and password.
vk.acquireToken('[email protected]', 'password');
vk.on('appServerTokenReady', function() {
vk.request('acquireTokenReady');
// etc
});
vk.on('acquireTokenNotReady', function(_error) {
// error handler
});
vk.request('getProfiles', {'uids' : '29894'});
vk.on('done:getProfiles', function(_o) {
console.log(_o);
});
There are two ways to get response: event and callback function.
When request result will be ready, SDK will fire event with request result. Event name will be like done:methodName. So if you request getProfiles() SDK will fire done:getProfiles event();
But you can set your custom event name:
vk.request('getProfiles', {'uids' : '29894'}, 'myEvent1');
vk.on('myEvent1', function(_o) {
console.log(_o);
});
vk.request('getProfiles', {'uids' : '1'}, 'myEvent2');
vk.on('myEvent2', function(_o) {
console.log(_o);
});
When request result will be ready, SDK will call callback function with request result. For this, you need to specify callback with 3rd parameter of request.
Example:
vk.request('getProfiles', {'uids' : '29894'}, function(_o) {
console.log(_o);
});
You can't change the names of this events.
- tokenByCodeReady
- tokenByCodeNotReady
- appServerTokenReady
- appServerTokenNotReady
- acquireTokenReady
- acquireTokenNotReady
- acquireToken(login, password) - request token by login and password
- setToken([params]) — request token using code from client-side
- changeMode(string) — set up request mode (oauth or sig)
- getToken() — get current token
- request(methodName, methodParams, [response], responseType) — request API method
SDK provides all methods from events.EventEmitter
- [email protected]
- skype: andrey.korchak
- http://57uff3r.ru
- http://vk.com/s7uff3r
See also vk.com cities and counties DB