GmailAPI:“向 Cloud PubSub 项目/[project-id]/topics/[topic-id] 发送测试消息时出错:用户无权执行此操作。”?

Dar*_*eth 2 python gmail push-notification gmail-api google-cloud-pubsub

我正在使用 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."


到目前为止,我已经做了以下事情:

  • 创建主题
  • 创建订阅
  • 手动发送和接收消息
  • 创建一个服务帐户,具有“所有者”和“Cloud Pub/Sub 服务代理”权限
  • 对于该主题,将服务帐户设置为所有者
  • 对于订阅,将服务帐户设置为所有者

就代码而言,我只是添加到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()


Run Code Online (Sandbox Code Playgroud)

编辑:

  1. 我在 Windows 笔记本电脑上的 Visual Studio Code 中运行它。我只是打开了一个新的 Python 文件。
  2. 这是 @gmail.com 帐户,而不是 GSuite 帐户
  3. 我正在使用服务帐户进行身份验证(creds对象)

编辑2: 我添加了带有上述凭据的完整文件。

Edo*_*kse 5

更新的答案

我已经测试了您的代码,并且遇到了同样的问题,但这是因为我故意跳过了一些步骤来弄清楚在某些阶段会发生什么。

当我没有服务帐户添加gmail-api-push@system.gserviceaccount.com为相关主题的发布者时,我得到了您的确切问题。

请原谅我糟糕的绘画技巧: 在此输入图像描述


原答案

所以问题是,您正在尝试使用服务帐户来验证 @gmail.com 帐户的 Gmail API 的 API 调用,我猜测遵循此示例

唯一可行的方法是,如果您使用的帐户是 GSuite 帐户并且您正在使用域范围委派。本文档(针对 Admin SDK,但其工作方式是相同的)解释了其工作原理。它不适用于您,但为了完整起见,我添加了此内容。

服务帐户不能用于验证 @gmail.com 帐户的请求。

它们用于验证自己的 GSuite 相关 API,例如 GDrive、GDocs 等,但它们基本上用于验证自己的帐户,而不是 @gmail.com 帐户。我不会详细介绍这一点,因为这并不完全相关,而且这是一个有点奇怪的兔子洞,即使爱丽丝也无法摆脱。

要将 Gmail API 与您的 @gmail.com 帐户一起使用,您需要按照您链接的快速入门中的指示创建 creds 对象,以便使用您的 @gmail.com 帐户的凭据对请求进行身份验证。您只需执行一次此操作,并将生成的 JSON 凭据文件保存在某处,以便您可以重复使用它。它将一直有效,直到您撤销它为止。

请注意,文档还指出您需要使用标准 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())
Run Code Online (Sandbox Code Playgroud)

您可以使用这部分将凭据加载到 creds 对象中,然后只需使用您已有的代码就可以了...

    if os.path.exists('token.json'):
        creds = Credentials.from_authorized_user_file('token.json', SCOPES)
Run Code Online (Sandbox Code Playgroud)

我不能 100% 确定您是否需要授予 GCloud 项目本身的权限,但您可以通过完成快速入门的其余部分来测试 Gmail 信用对象是否有效,并查看是否可以获得 @gmail 的标签。 com 帐户。如果此方法有效,并且您仍然收到 403 错误,请尝试向 @gmail.com 帐户授予您向服务帐户授予的 Pub/Sub IAM 权限。