檢索訪問令牌,我提出以下要求:無需人工複製
檢索訪問令牌。此網址導致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:https://vk.com/dev/access_token – jackdawrites
難道你不能改變redirect_uri嗎?通常的做法是打開一個本地web服務器(比如在'localhost:5000'上)並重定向用戶,然後一旦收到該令牌就停止服務器。 –
@Rawing謝謝,我會盡力的。 – jackdawrites