问题描述
我正在使用 Gmail API,并尝试使用 Python 3.9 设置推送通知。当我尝试在 Gmail 收件箱中调用 watch() 时出现错误,即使我已经遵循了针对类似问题给出的所有建议。错误如下:
"Error sending test message to Cloud PubSub projects/[project-id]/topics/[topic-id] : User not authorized to perform this action."
到目前为止,我已经完成了以下工作:
在代码方面,我只是添加到 Google 提供的 Python Quickstart 文件 (https://developers.google.com/gmail/api/quickstart/python) 中。这是我添加的内容:
from __future__ import print_function
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
# If modifying these scopes,delete the file token.json.
ScopES = ['https://www.googleapis.com/auth/gmail.readonly']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.json stores the user's access and refresh tokens,and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json',ScopES)
# If there are no (valid) credentials available,let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json',ScopES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json','w') as token:
token.write(creds.to_json())
#NEW CODE I ADDED FOR watch()
service = build('gmail','v1',credentials=creds)
request = {
'labelIds': ['INBox'],'topicName': 'projects/[project-id]/topics/[topic-id]'
}
service.users().watch(userId='me',body=request).execute()
if __name__ == '__main__':
main()
编辑:
- 我在 Windows 笔记本电脑上使用 Visual Studio Code 运行它。我只是打开了一个新的 Python 文件。
- 这是一个@gmail.com 帐户,而不是 GSuite 帐户
- 我正在使用服务帐户进行身份验证(creds 对象)
解决方法
更新答案
我已经测试了您的代码,并且遇到了同样的问题,但这是因为我故意跳过步骤来弄清楚在某些阶段会发生什么。
当我没有将服务帐户 gmail-api-push@system.gserviceaccount.com
添加为相关主题的发布者时,我遇到了您的确切问题。
原答案
因此,问题在于您正在尝试使用服务帐户来验证对 @gmail.com 帐户的 Gmail API 的 API 调用,我猜是在关注 this example。
唯一可行的方法是,如果您使用的帐户是 GSuite 帐户并且您使用的是域范围委派。 This doc(对于 Admin SDK,但它的工作方式是相同的)解释了它是如何工作的。它不适用于您,但为了完整起见,我添加了它。
服务帐户不能用于对@gmail.com 帐户的请求进行身份验证。
它们可用于验证自己的 GSuite 相关 API,例如 GDrive、GDocs 等,但它们基本上用于验证自己的帐户,而不是 @gmail.com 帐户。我不会详细说明这个,因为这并不完全相关,而且这是一个即使爱丽丝也无法摆脱的奇怪的兔子洞。
要将 Gmail API 与您的 @gmail.com 帐户一起使用,您需要按照快速入门 you linked 中的说明创建 creds 对象,以便请求获得 authenticated using the credentials for your @gmail.com account。您只需执行一次,并将生成的 JSON 凭证文件保存在某处,以便您可以重复使用它。它将一直有效,直到您撤销它。
请注意,the docs 还声明您需要使用标准 Gmail API 身份验证。
以下部分创建了 JSON 文件,您可以使用该文件使用您登录时使用的 @gmail.com 帐户验证对 Gmail API 的请求。
# Save the credentials for the next run
with open('token.json','w') as token:
token.write(creds.to_json())
您可以使用此部分将凭据加载到 creds 对象中,然后只需使用您已有的代码即可...
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json',SCOPES)
我不是 100% 确定您是否需要授予 GCloud 项目本身的权限,但是您可以通过完成快速入门的其余部分来测试 Gmail 信用对象是否有效,看看您是否可以获得 GCloud 项目的标签@gmail.com 帐户。如果这样做有效,并且您仍然收到 403 错误,请尝试为 @gmail.com 帐户授予您授予该服务帐户的 Pub/Sub IAM 权限。