【问题标题】:Gmail Settings API Backend error - PythonGmail 设置 API 后端错误 - Python
【发布时间】:2016-11-30 13:00:52
【问题描述】:

上个月,我完成了一个使用 Python 的 GAE 应用程序的工作,该应用程序广泛使用各种 Google API 来由 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()  

在这个特定的示例中,我正在尝试更新 AutoForwarding 设置,但它与某些 send-as 设置的场景和错误相同。我遇到的问题如下;对于谷歌所说的“精细设置”,我需要使用范围:'https://www.googleapis.com/auth/gmail.settings.sharing',它需要创建一个服务帐户才能工作。

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

HttpError:https://www.googleapis.com/gmail/v1/users/me/settings/autoForwarding?alt=json 返回“后端错误”>

如果我正在验证对服务帐户的域范围访问权限,为什么会出现此错误,这是 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 范围内的任何设置进行任何更改时,我都会收到后端错误消息。这让我发疯,我刚刚完成了这个应用程序!!!!如果您有任何想法或以前遇到过这个问题,请告诉我!!!!!!! ...

【问题讨论】:

    标签: python google-app-engine gmail-api google-admin-sdk google-admin-settings-api


    【解决方案1】:

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

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

    【讨论】:

    • Eric,感谢您的回复...我想了很多,因为我尝试了 3 种不同的授权方法。我会密切关注这个问题,我确实注意到虽然他们只提到转发更新发生这种情况,但它实际上发生在'googleapis.com/auth/gmail.settings.sharing; 范围内的每个服务/资源中。如果您是从事此工作的团队的一员,请查看该特定范围内的这些资源;但话说回来,直接的问题是授权问题……希望很快就有解决方案。
    • 我们昨天推出了一个修复程序,应该可以解决这个问题。注意:您必须通过服务帐户使用域范围委派才能使用需要 gmail.settings.sharing 范围的方法。
    • Eric,非常感谢您在修复推出后的及时回复!!!我将实现我需要的属于该范围的功能,并让你们知道我是否面临任何其他相关问题。当然,我们已经遵守了这些要求(即具有域范围委派的服务帐户),但再次感谢您的提醒。
    猜你喜欢
    • 1970-01-01
    • 2020-05-08
    • 1970-01-01
    • 2015-10-20
    • 2014-11-12
    • 2018-07-25
    • 2015-05-29
    • 2018-06-04
    • 2017-09-23
    相关资源
    最近更新 更多