1

如何在Python中使用訪問令牌來調用Google+ API,目前我使用的是首先交換身份驗證網址然後交換憑證的流動方法。使用令牌致電Google + API

我的代碼是這樣的:

FLOW = OAuth2WebServerFlow(
     client_id=client id, 
     client_secret=secret, 
     scope=scope, 
     user_agent=user_agent,redirect_uri=redirect_uri) 

auth_uri = FLOW.step1_get_authorize_url() 



credentials=FLOW.step2_exchange(code) 


people_service = apiclient.discovery.build('people', 'v1',credentials=credentials) 
connections = people_service.people().connections().list(resourceName='people/me').execute() 

回答

0

您需要實際訪問auth_uri網頁,用戶將登錄,並得到一個驗證碼,它會重定向回您的應用程序。
在非瀏覽器應用程序中,您可以執行此帶外操作(OOB)並使用redirect_uri='urn:ietf:wg:oauth:2.0:oob',並且您可以將print(auth_uri),剪切爲&將其粘貼到瀏覽器地址欄並輸入您的用戶憑據。

當您獲得驗證碼時,您需要在上述腳本中將其分配給code,例如, (for OOB):

code = input("Code: ") # raw_input() in Py2 

然後你的代碼的其餘部分將工作正常。

我會考慮查看oauthclient.<type>.Storage模塊來存儲從FLOW.step2_exchange()收到的憑證。所以你不一定需要每次都做這個交換。

+0

是的,我知道這段代碼工作正常。我需要做的是用訪問令牌調用谷歌apis,因爲我的android應用程序正在生成訪問令牌,並且我想使用該訪問令牌。 –