U
U
user.2019-05-02 23:35:55
Python
user., 2019-05-02 23:35:55

Who has worked in python with the vk API using the 'vk' library?

Good evening,
I understand the vk library in python, and I can’t understand what’s the matter and why it refuses to work for me. The example below is taken from the net and should work as far as I understand without a token, but for some reason it does not work, showing the following errors.
Installed: pip install vk

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']):  # если появился в сети, то выводим время
        now = datetime.datetime.now()
        print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
        print('Появился в сети в: ', now.strftime("%d-%m-%Y %H:%M"))
        return True
    if (current_status) and (not profiles[0]['online']):  # если был онлайн, но уже вышел, то выводим время выхода
        print('Вышел из сети: ', datetime.datetime.fromtimestamp(profiles[0]['last_seen']['time']).strftime('%d-%m-%Y %H:%M'))
        print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
        return False
    return current_status

if __name__ == '__main__':
    id = input("ID пользователя: ")
    session = vk.Session()
    vk_api = vk.API(session)
    current_status = False
    while(True):
        current_status = get_status(current_status, vk_api, id)
        sleep(60)

run and enter user id:
ID user: 1
Traceback (most recent call last):
  File "test.py", line 24, in <module>
    current_status = get_status(current_status, vk_api, id)
  File "test.py", line 6, in get_status
    profiles = vk_api.users.get(user_id=1, fields='online, last_seen')
  File "/usr/local/lib/python2.7/dist-packages/vk/api.py", line 173, in __call__
    return self._api._session.make_request(self)
  File "/usr/local/lib/python2.7/dist-packages/vk/api.py", line 102, in make_request
    raise error
vk.exceptions.VkAPIError: 5. User authorization failed: no access_token passed.. request_params = {u'oauth': u'1', u'fields': u'online, last_seen', u'user_id': u'1', u'method': u'users.get'}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Ogai, 2019-05-02
@nekolov

The users.get method has long required a token. Link: click

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question