![](/img/trans.png)
[英]Response status code 400 when using python module Requests with Sitescout API
[英]Python Requests - Azure RM API returning 200 status code but 400 in content
我有一些代碼要在其中嘗試針對Azure的Resource Manager REST API進行身份驗證。
import json
import requests
tenant_id = "TENANT_ID"
app_id = "CLIENT_ID"
password = "APP_SECRET"
token_endpoint = 'http://login.microsoftonline.com/%s/oauth2/token' % tenant_id
management_uri = 'https://management.core.windows.net/'
payload = { 'grant_type': 'client_credentials',
'client_id': app_id,
'client_secret': password
}
auth_response = requests.post(url=token_endpoint, data=payload)
print auth_response.status_code
print auth_response.reason
返回:
200
OK
但是,當我打印auth_response.content或auth_reponse.text時,我得到了400 HTML錯誤代碼和錯誤消息。
HTTP Error Code: 400
Sorry, but we’re having trouble signing you in.
We received a bad request.
但是,我可以使用PostMan使用相同的URI和有效負載來獲取正確的信息。 我使用Postman中的“生成代碼”選項將我的請求導出到Python請求腳本,並嘗試運行該腳本。 但是,我得到同樣的錯誤。
有人知道為什么會這樣嗎?
對於token_endpoint,應使用HTTPS而不是HTTP,並且還應指定API版本。 這是您應該使用的。
token_endpoint = 'https://login.microsoftonline.com/%s/oauth2/token?api-version=1.0' % tenant_id
僅將您的token_endpoint
修改為https
協議。 EG: token_endpoint = 'https://login.microsoftonline.com/%s/oauth2/token' % tenant_id
。 您可以參考https://msdn.microsoft.com/en-us/library/azure/dn645543.aspx了解更多詳細信息。
同時,您可以利用適用於Python的Microsoft Azure Active Directory身份驗證庫(ADAL)輕松獲取訪問令牌。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.