Git Product home page Git Product logo

node-vkapi's Introduction

Node.js SDK for VKontakte API

'use strict';

const VKApi = require('node-vkapi');
const VK    = new VKApi();

VK.call('users.get', {
  user_ids: '1'
}).then(res => {
  console.log(res);
});

Installation

$ npm install node-vkapi --only=prod --save

Features

  • Calling all VK API methods
  • Getting user access_token using:
    1. Login and password (dirty way)
    2. Login and password (via official Android app)
  • Getting server access_token
  • Uploading files to vk.com
  • Recognizing captcha

Example

const VKApi = require('node-vkapi');
const VK    = new VKApi({
  app: {
    id: 1234567890,
    secret: 'app-secret-key'
  }, 
  auth: {
    login: '+79871234567', 
    pass: 'password123'
  }
});

VK.auth.user({
  scope: ['audio', 'photos', 'friends', 'wall', 'offline']
}).then(token => {
  return VK.call('wall.post', {
    owner_id: token.user_id, 
    friends_only: 0, 
    message: 'Post to wall via node-vkapi'
  }).then(res => {
    // wall.post response
    return 'https://vk.com/wall' + token.user_id + '_' + res.post_id;
  });
}).then(link => {
  // returned data from previous .then
  console.log('Post was published: ' + link);
}).catch(error => {
  // catching errors
  console.log(error);
});

API Reference

All methods, except vkapi.setOptions, return Promise(response).
Method vkapi.setOptions returns this.

new vkapi(options):

  • options (Object):
    • app (Object):
      • id (Number): Application ID
      • secret (String): Application secret-key
    • auth (Object):
      • login (String)
      • pass (String)
      • phone (String): Phone number (Example: +79991234567)
    • captcha (Object):
      • service (String): Captcha service (rucaptcha, anti-captcha, antigate). anti-captcha by default
      • key (String): Cpatcha service API-key
    • delays (Boolean): Enable delays (334ms) between requests? true by default
    • token (String): Access token
    • version (String): Latest VK API version by default

You must specify parameter auth only if you plan to receive access_token by the login and password.

vkapi.call(method, params):

  • method (String)
  • params (Object):
    • < .. method params .. >
    • v (String): vkapi.options.version by default
    • access_token (String): vkapi.options.token by default

If the parameter v was not passed, then v always be equal to the latest version of VK API.
You must specify parameter access_token if the VK API method requires it, but vkapi.options.token is null.

If ETIMEDOUT or similar error occurs, function does not return it and tries to resend a request with same params.

vkapi.auth.server():

Getting server access_token. More details: vk.com/dev/auth_server, vk.com/dev/secure

vkapi.auth.user(params):

  • params (Object):

If type === android, then access token will be got via official Android app (vk.com/dev/auth_direct).
Else access token will be gained "dirty way".

Before using this method recommended to provide a phone number in vkapi.options.auth.phone if login is an e-mail, because during authorization may occur "security check" when you have to verify your phone number by entering it in the field. Phone number must start with +.

If access_token was got successfully, it will be saved in vkapi.options.token.

vkapi.upload(type, params):

  • type (String): One of given types of uploads
  • params (Object):
    • data (Readable Stream): or Array of Readable Streams (only for photo_album type)
    • beforeUpload (Object): Request parameters for 1st API-call (getting upload url). See vk.com/dev/upload_files
    • afterUpload (Object): Request parameters for 2nd API-call (saving file). As example, you can specify artist and title params to save audiofile with certain title and artist. (See: vk.com/dev/audio.save)

Keep in mind, that to upload files you must have the appropriate permissions.

Types of uploads

  • audio
  • audio_msg *
  • video
  • document
  • graffiti *
  • photo_pm
  • photo_wall
  • photo_main
  • photo_album
  • photo_market
  • photo_market_album

Example of uploading

// upload 'photo_wall', then post it to own wall

'use strict';

const fs    = require('fs');
const VKApi = require('node-vkapi');
const VK    = new VKApi({
  token: 'access_token'
});

VK.upload('photo_wall', {
    data: fs.createReadStream('photo.png')
  })
  .then(r => {
    return VK.call('wall.post', {
      owner_id: r[0].owner_id, 
      attachments: 'photo' + r[0].owner_id + '_' + r[0].id
    }).then(res => {
      return 'https://vk.com/wall' + r[0].owner_id + '_' + res.post_id;
    });
  })
  .then(link => console.log('Your post with photo is here: ' + link))
  .catch(e => console.log(e));

vkapi.setOptions(options):

node-vkapi's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.