aiovkcom¶
aiovkcom is a python vk.com API wrapper. The main features are:
- authorization (Authorization Code, Implicit Flow)
- REST API methods
Usage¶
To use vk.com API you need a registered app and vk.com account. For more details, see aiovkcom Documentation.
from aiovkcom import TokenSession, API
session = TokenSession(access_token, v='5.101')
api = API(session)
events = await api.wall.get()
friends = await api.friends.get()
Pass access_token
that was received after authorization.
For more details, see
aiovkcom Documentation.
Supported Python Versions¶
Python 3.5, 3.6, 3.7 and 3.8 are supported.
Getting Started¶
Installation¶
If you use pip, just type
$ pip install aiovkcom
You can install from the source code like
$ git clone https://github.com/KonstantinTogoi/aiovkcom.git
$ cd aiovkcom
$ python setup.py install
Application¶
After signing up visit vk.com API documentation page and create a new application: https://vk.com/editapp?act=create.
Save client_id (aka app_id) and client_secret (aka app_secret) for user authorization.
app_id = 'your client id'
app_secret = 'your secret key'
Authorization¶
To authorize with vk.com OAuth 2.0 you need app_id
(Implicit Grant)
or app_id
and app_secret
(Code Grant).
The preferred way to authorize is an async with
statement.
After authorization the session will have the following attributes:
access_token
expires_in
user_id
Authorization Code Grant¶
from aiovkcom import CodeSession, API
app_id = 123456
app_secret = 'abc'
async with CodeSession(app_id, app_secret, code, redirect_uri) as session:
api = API(session)
...
About OAuth 2.0 Authorization Code Grant: https://oauth.net/2/grant-types/authorization-code/
For more details, see https://vk.com/dev/authcode_flow_user
Implicit Grant¶
from aiovkcom import ImplicitSession, API
app_id = 123456
app_secret = ''
async with ImplicitSession(app_id, login, passwd, scope) as session:
api = API(session)
...
About OAuth 2.0 Implicit Grant: https://oauth.net/2/grant-types/implicit/
For more details, see https://vk.com/dev/implicit_flow_user
Session¶
Request¶
The session makes GET requests when you call methods
of an API
instance.
For example, the following code block
from aiovkcom import TokenSession, API
session = TokenSession('abcde', v='5.101')
api = API(session)
news = await api.newsfeed.get()
is equivalent to GET request:
https://api.vk.com/method/newsfeed.get?access_token=abcde&v=5.101
Response¶
By default, a session after executing request returns response’s body (value of “response” key). For example, if original response looks like this:
{"response":[{"id":210700286,"first_name":"Lindsey","last_name":"Stirling"}]}
then the session will return
[{"id":210700286,"first_name":"Lindsey","last_name":"Stirling"}]
You can pass pass_error
parameter to TokenSession
for returning original response (including errors).
Error¶
In case of an error, by default, an exception is raised.
You can pass pass_error
parameter to TokenSession
for returning original error’s body.
For example:
{"error": {"error_code": 1, "error_msg": "Unknown error occurred", "request_params": { ... }}
REST API¶
List of all methods is available here: https://vk.com/dev/methods.
Executing requests¶
from aiovkcom import API
api = API(session)
events = await api.newsfeed.get() # events for current user
friends = await api.friends.get() # current user's friends
Under the hood each API request is enriched with parameters (https://vk.com/dev/api_requests):
access_token
v
to authorize request.