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