2

I'm testing the vk.com api to be able to perform a search. I can get the token, but when I use it, it always returns the error: "access_token has expired" Please can you give me a hand to see if the code is wrong, or is it a vk configuration?

Thank you very much

import vk
import requests

url = 'https://oauth.vk.com/access_token?client_id=myClient&client_secret=mySecret&v=5.65&grant_type=client_credentials'
request = requests.get(url).json()
access_token = request["access_token"]

session = vk.Session(access_token)
api = vk.API(session)
search = api.search.getHints(q='python')

The error:

File "C:\Users\gabri\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vk\api.py", line 173, in __call__
    return self._api._session.make_request(self)
  File "C:\Users\gabri\AppData\Local\Programs\Python\Python36-32\lib\site-packages\vk\api.py", line 102, in make_request
    raise error
vk.exceptions.VkAPIError: 28. Application authorization failed: access_token has expired.. request_params = {'oauth': '1', 'method': 'search.getHints', 'q': 'python'}
SiHa
  • 7,830
  • 13
  • 34
  • 43
Gabriel Sule
  • 373
  • 6
  • 17
  • read more about [this API method](https://vk.com/dev/search.getHints): you should grant access to **friends and groups** – Azat Ibrakov Jun 14 '17 at 13:09

1 Answers1

7

we can manually obtain token following URL

https://oauth.vk.com/authorize?client_id=APPLICATION_CLIENT_ID&display=page&redirect_uri=https://oauth.vk.com/blank.html&scope=SCOPE&response_type=token&v=5.65

where

  • APPLICATION_CLIENT_ID is one of your applications ID, full list of managed applications can be found here

  • SCOPE is a list of comma separated permissions

if we take a look at documentation about search.getHints method we will find out that

This method can be called with a user token received in Standalone-app via Implicit Flow. Access rights required: friends and groups.

so our SCOPE may be like friends,groups (also we can add offline permission to make "eternal" token, but it is not safe)

So our URL for generating token to application with id = 1 may look like

https://oauth.vk.com/authorize?client_id=1&display=page&redirect_uri=https://oauth.vk.com/blank.html&scope=friends,groups&response_type=token&v=5.65

after following this link there will be form like

accessing request from application

we must check that it is our application and click Allow, after that we will be redirected and token will be in redirected page URL from which we can finally get our token:

redirect

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50