Ruby library for authorization of client applications and for access to the VK API
Add to your Gemfile
:
gem 'vkontakte_client'
Then bundle install
require 'vkontakte_client'
You must register your application in order to use all the capabilities of API VKontakte.
Open the page “Managed apps” in the left menu, then press “Create an app”. You will be directed to the page https://vk.com/editapp?act=create.
You need to choose Standalone-app.
After confirming the action, you will be redirected to the page with information about the app. Open the page "Settings" in the left-hand menu and you will see the field "ID applications", in which a number will be located. For example, 5490057
. This number is the application identification, a.k.a. API_ID
, APP_ID
, client_id
, and you will need it for your future work.
With client authorization, the access key to the API access_token
.
The constructor takes only one argument - the VK application ID - CLIENT_ID
.
vk = Vkontakte::Client.new(CLIENT_ID)
In general, for API identification, a special access key is used which is called access_token
. This token is a string of numbers and Latin letters which you send to the server along with the request.
This library supports the Implicit flow way to obtain an OAuth 2.0 access key: th 2.0:
The login!
method takes the following arguments:
email
: user loginpass
: user passwordpermissions
: request application permissions
vk.login!(email, pass, permissions: 'friends')
After successful authorization, you can make requests to the API using the method name from the API function list.
The parameters of the corresponding API method are passed as Hash
.
Note that a method like friends.get
needs to be passed as friends_get
.
vk.api.friends_get(fields: 'online', order: 'name', name_case: 'dat')
To avoid errors, you can pre-check the status of the user using the account.getInfo
method.
vk.api.account_getInfo
It is useful to save the received access_token
(and, if necessary, the user_id
) to reuse them
access_token = vk.access_token
user_id = vk.user_id
api = Vkontakte::API.new(access_token)
api.friends_get(fields: 'online', order: 'name', name_case: 'dat')
- Fork it (https://github.com/mamantoha/vkontakte_client/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Copyright: 2010-2023 Anton Maminov (anton.maminov@gmail.com)
This library is distributed under the MIT license. Please see the LICENSE file.