我正在尝试请求授权码 documented here 。 我使用 Python requests 包来执行此操作,并具有以下示例代码:
import requests
auth_endpoint = 'https://login.microsoftonline.com/%s/oauth2/authorize?api-version=1.0' % TENANT_ID
payload = {
'client_id': CLIENT_ID,
'response_type': 'code',
'resource': APP_ID_URI,
'redirect_uri': REPLY_URL
}
response = requests.get(url=auth_endpoint, data=payload)
但是,当我运行上面的代码时,我在正文中返回了 HTML,而不是我期望的响应。 HTML 代码似乎是用于登录页面的。 当我获取格式化的端点 URI 并将其插入浏览器时,我可以从重定向 URI 获取身份验证代码。但是,有没有办法在仍然使用请求包的同时从响应正文中获取此信息?
最佳答案
请使用requests模块的session类来实现您的需求。请引用以下代码示例:
import requests
s = requests.Session()
USERNAME = '<username_email>'
PASSWORD = '<userpassword>'
s.auth = (USERNAME, PASSWORD)
TENANT_ID = '<tenant_id>'
# Authorize URL
authorize_url = 'https://login.microsoftonline.com/%s/oauth2/authorize' % TENANT_ID
# Token endpoint.
token_url = 'https://login.microsoftonline.com/%s/oauth2/token' % TENANT_ID
payload = { 'response_type': 'code',
'client_id': '<tenant_id>',
'redirect_uri': 'http://localhost',
'authority' :'authority'
}
response = s.get(authorize_url, params=payload ,allow_redirects=True)
print response
print response.url
如有任何进一步的疑问,请随时告诉我。
关于python - Azure 访问 token 请求返回 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37885574/