Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import urllib
- import json
- from formdata import *
- import urllib2
- class VKClientException(Exception): pass
- class VKClient:
- def __init__(self):
- self.client_id = '2692017'
- self.client_secret = 'rqIkXYBauPjqE2EVxB2j'
- self.permissions = 'notify,friends,photos,audio,video,docs,notes,wall,groups,messages,notifications,activity,status,pages,stats'
- self.api_version = '5.3'
- self.auth_info = None
- def auth(self, login, password, captcha_sid='', captcha_key=''):
- payload = {
- 'username': login,
- 'password': password,
- 'captcha_sid': captcha_sid,
- 'captcha_key': captcha_key,
- 'client_id': self.client_id,
- 'client_secret': self.client_secret,
- 'scope': self.permissions,
- 'grant_type': 'password',
- 'v': self.api_version
- }
- r = urllib.urlopen('https://oauth.vk.com/token?' + urllib.urlencode(payload));
- if r.code != 200:
- raise VKClientException('HTTP status code was not 200')
- self.auth_info = auth_info = json.loads(r.read())
- if 'error' in self.auth_info:
- raise VKClientException('%s: %s' % (auth_info['error'], auth_info['error_description'] if 'error_description' in auth_info else ''))
- def api_call(self, method, data):
- if type(data) == dict:
- data_type = 'application/x-www-form-urlencoded'
- data['access_token'] = self.auth_info['access_token']
- data = urllib.urlencode(data)
- elif isinstance(data, FormData):
- data_type = 'multipart/form-data; boundary=' + data.boundary
- data.add_text('access_token', self.auth_info['access_token'].encode('utf-8'))
- data = str(data)
- else:
- raise VKClientException('Bad data')
- req = urllib2.Request('https://api.vk.com/method/' + method, data, {'Content-Type': data_type})
- res = urllib2.urlopen(req)
- # print res.info().headers
- return json.loads(res.read().decode('utf-8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement