
Hi, Habr! This article is intended for those who want to understand the basics of the VK API in Python, since there are no articles on this (there is one article on Habré, but it is not completely relevant, as some methods do not work), but on other resources managed to find only user questions, but no guides and other things.
There are two popular libraries for working with VK API in Python:
vk and
vk_api . I won’t try to judge which of the libraries, but I’ll say one thing: vk has too little documentation (therefore, I understood it practically) and English, and vk_api has more documentation (therefore I don’t see any reason to write about this library). For me, the main thing is not in what language the documentation is, but for some users it is of great importance when choosing.
As you already understood, this article discusses working with the vk library.
This library is installed with the following standard command:
')
pip install vk
After the module is installed, we need to create an application on the social network site. I think that most users can do this, so I’ll skip the information on this step. If someone does not know how, then google, do not be shy.
After registering the application, we will need only its ID.
Let's start with the authorization. In principle, some information can be obtained without entering personal data, which is of course good, for example:
import vk session = vk.Session() vk_api = vk.API(session) vk_api.users.get(user_id=1)
Thus, we will get the last name, first name and id of the user with user_id = 1. If you need to get some more information about the user, then in the method call you need to specify additional fields, information about which should be returned:
vk_api.users.get(user_id=1, fields='online, last_seen')
Those. in this case, we will receive not only information about the user's first and last name with id = 1, but also information about whether the user is currently on the site (fields = 'online') and the last visit, as well as the device type (fields = ' last_seen ').
Actions without authorization do not provide us with the ability to use the VK API at full capacity, so we will consider authorization with the input of personal data. There are two ways: entering a login and password, entering a token. To log in using a token, you need to slightly supplement the first example, namely this line:
session = vk.Session(access_token='tocken')
Then everything remains the same as it was before, without any changes.
The next authorization method is to enter a username and password. In this case, too, everything is quite simple and clear:
session = vk.AuthSession('id_app', 'login', 'pass') vk_api = vk.API(session)
As you can see, nothing complicated and everything is so simple and clear that it does not even need additional comments.
With this authorization, you need to specify not only the login, password and application ID, but what we want to access.
For example, we do not have access to the user's wall now, so when we try to add an entry to the wall, we get an error:
vk_api.wall.post(message="hello") <b>: vk.exceptions.VkAPIError: 15.</b>
In order for this code to work correctly, when authorizing, you need to additionally specify an argument with the name scope and list, comma-separated, those methods that we want to access.
session = vk.AuthSession('id_app', 'login', 'pass', scope='wall, messages') vk_api = vk.API(session) vk_api.wall.post(message="hello")
In this example, I am requesting access to the wall and messages. The program execution is completed correctly, and an entry with the text 'hello' appears on the wall. The names of the methods that can be accessed can be found
on this documentation page .
That's all. Calling methods occurs on one pattern:
vk_api..(=) : vk_api.messages.send(users_id=0, messages='hello')
Thus, we send a hello message to the user with id = 0 (that is, to himself). The names of the parameters that need to be passed when calling any method can be found in the documentation, in the description of the method itself.
For a more visual work of the library, I implemented a small program that monitors when the user has logged into the VC, and when he has left it (it is hard to believe, but it may be of interest to anyone). The program code is below and on
GitHub .
Sample program using the vk library import datetime from time import sleep import vk def get_status(current_status, vk_api, id): profiles = vk_api.users.get(user_id=id, fields='online, last_seen') if (not current_status) and (profiles[0]['online']):
This article was intended only to understand the basics of working with the VK API in Python using the VK library.
All good!