'use strict';
const VKApi = require('node-vkapi');
const VK = new VKApi();
VK.call('users.get', {
user_ids: '1'
}).then(res => {
console.log(res);
});$ npm install node-vkapi --only=prod --save
- Calling all VK API methods
- Getting user
access_tokenusing:- Login and password (dirty way)
- Login and password (via official Android app)
- Getting server
access_token - Uploading files to vk.com
- Recognizing captcha
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);
});All methods, except vkapi.setOptions, return Promise(response).
Method vkapi.setOptions returns this.
options(Object):app(Object):id(Number): Application IDsecret(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-captchaby defaultkey(String): Cpatcha service API-key
delays(Boolean): Enable delays (334ms) between requests?trueby defaulttoken(String): Access tokenversion(String):Latest VK API versionby default
You must specify parameter auth only if you plan to receive access_token by the login and password.
method(String)params(Object):< .. method params .. >v(String):vkapi.options.versionby defaultaccess_token(String):vkapi.options.tokenby 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.
Getting server access_token.
More details: vk.com/dev/auth_server, vk.com/dev/secure
params(Object):type(String):androidornullscope(String or Array): Permissions (vk.com/dev/permissions)
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.
type(String): One of given types of uploadsparams(Object):data(Readable Stream): or Array of Readable Streams (only forphoto_albumtype)beforeUpload(Object): Request parameters for 1st API-call (getting upload url). See vk.com/dev/upload_filesafterUpload(Object): Request parameters for 2nd API-call (saving file). As example, you can specifyartistandtitleparams 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.
audioaudio_msg*videodocumentgraffiti*photo_pmphoto_wallphoto_mainphoto_albumphoto_marketphoto_market_album
// 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));options(Object): Constructor object