为什么在执行转发电子邮件的代码时出现访问限制错误?

Why am I getting error for access restriction when performing code for forwarding emails?

我是 Google API 的新手,目前正在使用代码添加转发电子邮件地址。代码如下

credentials = get_credentials(request.data['code'], request.data['state'])
service = build_gmail_service(credentials)
address = {'forwardingEmail': 'abcd1234@mydomain.com'}
result = service.users().settings().forwardingAddresses().create(userId='me', body=address).execute()

我可以使用上面的代码构建 google 服务,但是当执行设置转发电子邮件地址的最后一行时,我遇到了以下错误。

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/gmail/v1/users/me/settings/forwardingAddresses?alt=json returned "Access restricted to service accounts that have been delegated domain-wide authority">

我已经搜索了一天的解决方案,但无法解决,如果有其他人遇到同样的问题并解决了,请告诉我。

Gsuite 域智能委派图像:

谢谢。

Access restricted to service accounts that have been delegated domain-wide authority

它告诉您您正在尝试使用服务帐户连接到管理设置 api,但尚未从 gsuite 正确设置服务帐户的域范围委托。

您的 gsuite 管理员需要先设置对您的服务帐户的委派,然后您才能使用它。

Delegating domain-wide authority to the service account