Skip to content

lawrentiy/nodejs-vksdk

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

nodejs-vksdk

Small SDK for vk.com API.

Installation

npm install vksdk

Usage

var VK = require('vksdk');

You can use two ways of performing API requests:

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');

How to setup version of API and language

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'
});

Signature auth

You need just your appID and appSecret.

Token auth

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('vk_com_login@mail.com', 'password');
vk.on('appServerTokenReady', function() {
    vk.request('acquireTokenReady');
    // etc
});
vk.on('acquireTokenNotReady', function(_error) {
    // error handler
});

Requests

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.

Event

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);
});

Callback

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);
});

System events in SDK

You can't change the names of this events.

  • tokenByCodeReady
  • tokenByCodeNotReady
  • appServerTokenReady
  • appServerTokenNotReady
  • acquireTokenReady
  • acquireTokenNotReady

Methods

  • 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

Support

See also vk.com cities and counties DB

About

NodeJS SDK for vk.com

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.5%
  • Makefile 0.5%