Gmail 设置 API 后端错误 - Python

Gmail Settings API Backend error - Python

上个月我完成了一个使用 Python 的 GAE 应用程序,它广泛使用各种 Google APIs 来管理公司域内的 google 资源由 google 管理员提供。该应用程序已完成!!!,但本月 google 宣布不再支持我目前正在实施的 EmailSettings API,电子邮件设置将迁移到 Gmail API;到目前为止,他们已经迁移了一些设置(即发送别名、转发、假期回复和签名)。在 google 放在一起的迁移文档中,他们指出了两个 API 之间的主要区别以及关于如何迁移它的有点模糊的参考。无论如何,我目前正在尝试实施新的 API 以使用服务帐户修改发送方式设置。这是我为服务帐户创建服务的方式(同样,这是 Python):

scopes = ['https://mail.google.com/', 'https://www.googleapis.com/auth/gmail.settings.basic', 'https://www.googleapis.com/auth/gmail.settings.sharing']

email = "username@domain.bla"
credentials = oauth2.service_account.build_credentials(scope=scopes, user=me)       
http = httplib2.Http()
credentials.authorize(http)
service = google_api_helper.build("gmail", "v1", credentials)    
body = {'emailAddress':'anotheruser@domain.bla'}

service_2.users().settings().updateAutoForwarding(userId="me", body=body).execute()  

在这个特定示例中,我正在尝试更新自动转发设置,但它与某些发送方式设置的情况和错误相同。我遇到的问题如下;对于 "delicate settings" 作为 google 调用它们,我需要使用范围:'https://www.googleapis.com/auth/gmail.settings.sharing',它需要创建一个服务帐户才能工作。

每当我尝试使用它时,我都会收到一条 500 错误消息:

HttpError: https://www.googleapis.com/gmail/v1/users/me/settings/autoForwarding?alt=json returned "Backend Error">

为什么我在验证对服务帐户的全域访问权限时会收到此错误,这是一个 API 错误还是我目前正在实施 oauth2 身份验证的方式?我尝试了几种实现都没有成功:

使用应用程序默认身份验证:

credentials = GoogleCredentials.get_application_default()
httpauth = credentials.authorize(Http())
service = build("gmail", "v1", http=http_auth)
aliases_2 = service.users().settings().sendAs().list(userId="username@domain.bla").execute()

使用更新的 oauth2client 库并通过本地 json 文件:

 credentials_new = ServiceAccountCredentials.from_json_keyfile_name("app/service_account_key.json", scopes)
 delegated_credentials = credentials_new.create_delegated(sub="username@domain.bla")
 http_auth = delegated_credentials.authorize(Http())
 service = build("gmail", "v1", http=http_auth) 

使用过时的 oauth2client 库并使用新实现的库不再支持的 SignedJwtAssertionCredentials 函数:

credentials = SignedJwtAssertionCredentials(str(settings['oauth2_service_account']['client_email']).encode('utf-8'), settings['oauth2_service_account']['private_key'], scope=scopes, sub="username@domain.bla")
auth2token = OAuth2TokenFromCredentials(credentials) 
# With this implementation i was able to provide the google admin account which is supposed to have super admin access to the "sub" parameter, this used to work for the EmailSettings API, but for this new implementation you need to pass the email of the user you are trying to gain access to.
# build service
# call API

通过所有 3 种实现,我能够调用基本范围,但每当我尝试对 settings.sharing 范围下的任何设置进行任何更改时,我都会收到后端错误消息。这让我发疯,我刚刚完成了这个应用程序!!!!如果您有任何想法或者您之前对这个问题有过 运行,请告诉我!!!!!! ...

更新:自 2016 年 8 月 11 日起,此问题应已修复。

截至 2016-07-27,授权后端中存在导致此错误的错误,尽管它似乎只影响某些域/用户。我们正在努力修复。请加注星标 this issue 以获取更新。