Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

vksdk

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vksdk

SDK for API of vk.com

  • 4.7.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
198
increased by27.74%
Maintainers
1
Weekly downloads
 
Created
Source

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:

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

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

When you use token auth you also can call a few getters:

  • vk.getToken() — will return current token
  • vk.getUserId() — will return current user id
  • vk.getExpiresIn() — will return current token expiration time

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

HTTP errors

SDK emits 'http-error' event in case of http errors.

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

Keywords

FAQs

Package last updated on 08 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc