我一直在仅使用 python 请求获取端点来检索 Outlook 中的电子邮件
https://outlook.office365.com/api/v1.0/me/messages
示例代码:
import requests
requests.get('https://outlook.office365.com/api/v1.0/me/messages', auth=(email, pwd))
我会返回一个 json 对象,解析它,并获取我的电子邮件的内容。但现在 Microsoft 已弃用它,我一直在尝试迁移到使用 Microsoft Graph。我的问题是,如何获取 OAuth2 令牌而无需使用 http 请求启动浏览器?
到目前为止,我一直在阅读文档,并在应用程序注册门户中注册了我的“应用程序”(只是一个常规的Python脚本)。我遇到的每个例子,我总是必须访问一个授权网址,我必须通过前端 UI 手动登录,如下所示: 我希望能够通过 http 请求/没有前端 UI 来做到这一点,但我似乎找不到任何关于如何做到这一点的答案。
这是我到目前为止的代码:
import requests
authorize_url = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
token_url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
payload = {
'client_id': app_client_id, # Variable exists but not exposed in this question
'response_type': 'code',
'redirect_uri': 'https://login.microsoftonline.com/common/oauth2/nativeclient',
'response_mode': 'form_post',
'scope': 'mail.read',
'state': '12345'
}
r = requests.get(authorize_url, params=payload)
print(r.status_code)
print(r.text)
这就是我得到的回报:
200
<!DOCTYPE html>
<html dir="ltr" class="" lang="en">
<head>
<title>Sign in to your account</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scal able=yes">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<meta name="PageID" content="ConvergedSignIn" />
<meta name="SiteID" content="" />
<meta name="ReqLC" content="1033" />
<meta name="LocLC" content="en-US" />
<noscript>
<meta http-equiv="Refresh" content="0; URL=https://login.microsoftonline.com/jsdisabled" />
</noscript>
<link rel="shortcut icon" href="https://secure.aadcdn.microsoftonline-p.com/ests/2.1.8502.8/co ntent/images/favicon_a_eupayfgghqiai7k9sol6lg2.ico" />
<meta name="robots" content="none" />
...
这是我注册应用程序时平台设置中的内容(如果有帮助的话):
有什么方法可以通过编程获取授权码吗?我也尝试传递 auth 参数,但这不起作用。
我最近发现 python 请求可以处理 OAuth2,但现在我在尝试遵循 他们的示例时遇到了不同的错误。 这是我收到的错误:
File "outlook_test.py", line 31, in <module>
token = oauth.fetch_token(token_url=token_url, auth=auth)
File "C:\Users\ryee\Documents\gitLabQA\QA_BDD\outlook_env\lib\site-packages\requests_oauthlib\oauth2_session.py", line 307, in fetch_token
self._client.parse_request_body_response(r.text, scope=self.scope)
File "C:\Users\ryee\Documents\gitLabQA\QA_BDD\outlook_env\lib\site-packages\oauthlib\oauth2\rfc6749\clients\base.py", line 415, in parse_request_body_response
self.token = parse_token_response(body, scope=scope)
File "C:\Users\ryee\Documents\gitLabQA\QA_BDD\outlook_env\lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 425, in parse_token_response
validate_token_parameters(params)
File "C:\Users\ryee\Documents\gitLabQA\QA_BDD\outlook_env\lib\site-packages\oauthlib\oauth2\rfc6749\parameters.py", line 432, in validate_token_parameters
raise_from_error(params.get('error'), params)
File "C:\Users\ryee\Documents\gitLabQA\QA_BDD\outlook_env\lib\site-packages\oauthlib\oauth2\rfc6749\errors.py", line 405, in raise_from_error
raise cls(**kwargs)
oauthlib.oauth2.rfc6749.errors.InvalidClientIdError: (invalid_request) AADSTS90014: The required field 'scope' is missing.
Trace ID: 2359d8a6-0140-43c1-8ff5-8103045d2f00
Correlation ID: dff39a1f-ffe1-493e-aea3-1536974b777d
我尝试将
'[Mail.Read]'
作为范围,但收到“无效范围参数”。
我的新Python脚本:
from oauthlib.oauth2 import BackendApplicationClient
from requests.auth import HTTPBasicAuth
from requests_oauthlib import OAuth2Session
auth = HTTPBasicAuth(client_app_id, app_secret)
client = BackendApplicationClient(client_id=client_app_id)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url=token_url, auth=auth)
您使用的范围,Mail.Read 需要用户同意。您想要的是应用程序权限 Mail.Read ,它需要管理员同意(此链接显示如何获得管理员同意)。 您正在尝试访问用户的消息,因此用户需要同意该操作。 根据文档:-
我发现本教程对于获取访问代码非常有帮助:
本教程使用 Microsoft Graph(涵盖包括 Microsoft Outlook 在内的多种 Microsoft 产品)而不是 Outlook REST API(仅涵盖 Outlook)。
https://learn.microsoft.com/en-us/outlook/rest/python-tutorial
起初,我认为设置 Django 服务器太过分了。然后我意识到我需要一种方法让我的 Python 实例在完成单点登录后捕获访问代码。 (我必须使用浏览器进行单点登录,因为我的机构使用多重身份验证。)拥有 Django 服务器是实现此目的的自然方法。
因此,我创建了一个新的 PyCharm Django 项目(在 PyCharm 中很简单)并开始遵循教程。
我发现有必要继续按照教程显示我的电子邮件,以避免出现身份验证错误 - 偏离了教程,并且我收到了无法穿透的错误消息(例如这个)。
(我之前在here发布了这个答案来回答另一个问题。)