如何在没有 OAuth 同意屏幕的情况下使用 Gmail API?

问题描述 投票:0回答:1

我想使用 Gmail API 自动回复一些电子邮件。 该应用程序只能通过 Google OAuth 同意屏幕授权才能运行。 问题是令牌的时间是有限的,我需要程序在没有用户参与的情况下自行运行。 有没有办法使用服务帐户(但需要使用主电子邮件地址回复电子邮件)?或者以某种方式制作无限令牌?

我尝试使用服务帐户

SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']

SERVICE_ACCOUNT_FILE = 'service.json'

creds = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

service = build('gmail', 'v1', credentials=creds)
threads = service.users().threads().list(userId='me', q='is:unread').execute().get('threads', [])
print(threads)

但是返回了 401 Error 错误:

    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project."
python google-api google-oauth gmail-api
1个回答
0
投票

使用服务帐户作为同意屏幕的解决方法:

回答你的整个帖子。 0Auth Token Limited 保持原样,无法修改,或者没有直接的解决办法。此外,使用服务帐户的方法对您来说是一个可行的选择。您的问题很可能是由于域范围委派造成的。

解决方案:

设置您的域范围委托。

© www.soinside.com 2019 - 2024. All rights reserved.