Vk Documentation

advertisement
Vk Documentation
Release 2.0.2
Dmitry Voronin
August 21, 2016
Contents
1
Installation
1.1 Install via pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Install via setuptools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3
3
3
2
vk.com API
2.1 Making API request . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5
5
3
Usage
3.1 API method request example .
3.2 vk.API . . . . . . . . . . . .
3.3 vk.Session . . . . . . . . . .
3.4 vk.AuthSession . . . . . . . .
3.5 Debugging . . . . . . . . . .
.
.
.
.
.
7
7
7
7
8
8
4
Customizing
4.1 Session classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
9
5
Indices and tables
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
11
i
ii
Vk Documentation, Release 2.0.2
Contents:
Contents
1
Vk Documentation, Release 2.0.2
2
Contents
CHAPTER 1
Installation
Project is available on PyPI and Github:
1.1 Install via pip
pip install vk
1.2 Install via setuptools
Get package by one of these ways and install
• Download from PyPI page
• Clone Git repo
• Download Github release
python setup.py install
3
Vk Documentation, Release 2.0.2
4
Chapter 1. Installation
CHAPTER 2
vk.com API
To use vk.com API you need registered app and login in this social network.
https://vk.com/dev/main
vk.com developers guide:
1. Sign up in social network.
2. Go to https://vk.com/dev/standalone and create new app. Choose name and select standalone type.
3. Remember app id.
4. Use app id, list of required permissions and user credentials to get access token.
5. Use this access token to make method requests. List of all: https://vk.com/dev/methods. Some methods don’t
require access token.
2.1 Making API request
To make request to vk.com API we need send GET or POST HTTP request to address https://api.vk.com/method/method-name with params of specific method and access token.
5
Vk Documentation, Release 2.0.2
6
Chapter 2. vk.com API
CHAPTER 3
Usage
3.1 API method request example
Get user info with user id equal to 1.
>>> import vk
>>> session = vk.Session()
>>> api = vk.API(session)
>>> api.users.get(user_ids=1)
[{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}]
vk.API class object is used to create API request and send it via vk.Session class object. Session object is used by API
object to manage access token, send API request, get JSON response, parse and return it.
API object api attribute getting defines vk.com API method name. Call of gotten method sends request and returns
parsed JSON response. Keyword args becomes specified method params.
This example will send POST request to https://api.vk.com/method/users.get with “user_ids=1” query string.
3.2 vk.API
vk.API gets Session or subclass object as first argument, **kwargs as API method default args and timeout kwarg. See
https://vk.com/dev/api_requests for full list of common args. The most useful is v - API version and lang - language
of responses.
All API methods that can be called from server should be supported. See https://vk.com/dev/methods for detailed API
help.
session = vk.Session()
api = vk.API(session, v='5.35', lang='ru', timeout=10)
api.method.name(param=value)
3.3 vk.Session
vk.Session gets optional access_token argument. It will send access token with every API request after first “Autorization failed” error. Session class can use only ready access token and raises error if can’t get it.
7
Vk Documentation, Release 2.0.2
session = vk.Session(access_token='5ecre7')
api = vk.API(session)
...
3.4 vk.AuthSession
It’s vk.Session subclass. Can get access token using app id and user credentials.
session = vk.AuthSession(app_id='appid', user_login='jake@gmail.com', user_password='Finn')
api = vk.API(session)
...
3.5 Debugging
To understand that happens you can enable debug mode.
vk.logger is Python standard library logging.Logger instance.
8
Chapter 3. Usage
CHAPTER 4
Customizing
4.1 Session classes
Session classes are ready for customizing.
class vk.Session(access_token=None)
9
Vk Documentation, Release 2.0.2
10
Chapter 4. Customizing
CHAPTER 5
Indices and tables
• genindex
• modindex
• search
11
Vk Documentation, Release 2.0.2
12
Chapter 5. Indices and tables
Index
S
Session (class in vk), 9
13
Download