M
M
Mark Adams2016-04-22 14:57:17
Python
Mark Adams, 2016-04-22 14:57:17

How to implement VK API requests via Python without a token?

I have this: https://habrahabr.ru/sandbox/84639/
There, the request is made through vkapi.method, and I need to implement token-free requests without authorization. How to do it? Tried just import vk, vk.method - doesn't work.
Here's what to do now:

import urllib2, json, os.path

DIR = os.path.abspath(os.curdir) + '/'

def get(url, timeout=10):
  try:
    res = urllib2.urlopen(url, timeout=timeout).read()
  except:
    print 'get error'
    res = 0
  return res

def json_convert(string):
  return json.loads(string)

def groups_getById(group_ids=None, group_id=None, fields=None):
  url = 'https://api.vk.com/method/groups.getById?group_ids=' + str(group_ids) + '&group_id=' + str(group_id) + '&fields=' + str(fields)
  return json_convert(get(url))

def groups_getMembers(group_id=None, sort=None, offset=None, count=None, fields=None, filter=None):
  url = 'https://api.vk.com/method/groups.getById?&group_id=' + str(group_id) + '&sort=' + str(sort) + '&offset=' + str(offset) + '&count=' + str(count) + '&fields=' + str(fields) + '&filter=' + str(filter)
  return json_convert(get(url))

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
alex stephen, 2016-04-22
@berezuev

Close the article attached to the question and never read it. Install the Requests
library and use it to make requests. The list of methods is on vk.com/dev/methods

P
prawn-cake, 2016-05-27
@prawn-cake

Hello.
I recommend using the library https://github.com/prawn-cake/vk-requests

import vk_requests

api = vk_requests.create_api()
api.users.get(user_ids=1)
[{'first_name': 'Pavel', 'last_name': 'Durov', 'id': 1}]

But in general it is better to use token-based authentication, because token-free api is very limited

I
Ilya, 2016-04-22
@FireGM

import urllib
import urllib2, json, os.path

class Api():
    main_url = 'https://api.vk.com/method/'

    def method(self, method, params=None):
        if not params:
            params = {}
        full_url = self.generate_url(method, params)
        return urllib2.urlopen(full_url, timeout=timeout).read()

    def generate_url(self, method, params):
        m = self.main_url + method + "?" +urllib.urlencode(params)
        return m


api = Api()
res = api.method('users.get', {"user_ids" : 1, 'fields': "photo_50,city,verified"})

T
Taras Serevann, 2016-04-22
@Taras_Serevann

Maybe you should try making requests directly. Can you handle networking and JSON parsing? I'm sure there are plenty of ready-made high-level solutions for this.

M
Mark Adams, 2016-04-22
@ilyakmet

Found this: Vk api python without acess_token? But is there an option? I do not want to write all the functions again in python via urllib.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question