【发布时间】:2017-11-15 00:53:45
【问题描述】:
我提出以下要求:
检索访问令牌。这个 url 指向 vk.com 上的登录页面(如果尚未登录),然后提示用户授权应用程序,然后重定向到 https://oauth.vk.com/blank.html#access_token={token}&expires_in=0&user_id={id}。因此,要实际检索访问令牌,需要从地址栏中手动复制它。此过程在官方 API 中指定。有没有办法解决这个问题?如何仅使用 python 代码获取令牌?
下面是生成url的过程:
import requests
def authorize_app(client_id, redirect_uri = None):
'''
The function generates an url, which redirects to login page (optional, if not logged in) and app authorization.
'''
if redirect_uri == None:
redirect_uri = 'https://oauth.vk.com/blank.html' # default callback url
oauth = requests.get('https://oauth.vk.com/authorize', params = {'client_id' : str(client_id),
'redirect_uri' : redirect_uri,
'display' : 'page',
'scope' : ['friends,offline'], # offline option makes the token permanent
'response_type' : 'token',
'v' : 5.63})
return oauth.url
【问题讨论】:
-
API 文档和 oauth 的链接:vk.com/dev/access_token
-
你不能改变redirect_uri吗?通常的程序是打开一个本地网络服务器(比如
localhost:5000)并将用户重定向到那里,然后在收到令牌后停止服务器。 -
@Rawing 谢谢,我会试试的。
标签: python oauth python-requests vk