python - 使用 Google Apps 服务帐户启用邮件转发

标签 python google-api google-directory-api service-accounts google-email-settings-api

2015 年 4 月 20 日,多个 Google Apps API 将被停用,其中包括配置 API(gdata)。
在我的 Python 脚本中,我使用服务帐户和 OAuth 2.0,而不是 ClientLogin,以及替换 API:Directory API。
但是,我无法找到使用新 API 启用邮件转发的方法,并且所有 Google 邮件转发的 Python 文档都解释了如何使用 ClientLogin 来实现此操作,而 ClientLogin 也将于 4 月 20 日停止使用。

相关信息:
我有一个服务帐户并按照本指南对其进行了适当授权:https://developers.google.com/api-client-library/python/auth/service-accounts
我的所有其他功能都可以使用新的 API!
我已经彻底搜索了 Directory API 文档(尽管我不排除我错过了一些东西的可能性):https://developers.google.com/resources/api-libraries/documentation/admin/directory_v1/python/latest/index.html
Google 关于使用 Python 实现邮件转发的唯一文档(我发现)建议使用上面提到的 ClientLogin:https://developers.google.com/admin-sdk/email-settings/#manage_forwarding_settings

我现有的邮件转发工作代码(基于该文档):

client = gdata.apps.emailsettings.client.EmailSettingsClient(domain='mydomain.co')
client.ClientLogin(email=adminEmail, password=adminPass, source='apps')
client.UpdateForwarding(username=username, enable=True, 
    forward_to=forwardTo, action='ARCHIVE')

我根据 Jay Lee 的回答更新的代码:

credentials = SignedJwtAssertionCredentials(serviceEmail, key, sub=adminEmail, 
    scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/ '+'other scopes')
client = gdata.apps.emailsettings.client.EmailSettingsClient(domain='mydomain.co')
client.additional_headers = {'Authorization': 'Bearer %s' % credentials.access_token}
client.UpdateForwarding(username=username, enable=True, 
    forward_to=forwardTo, action='ARCHIVE')

我将新范围添加到我的服务帐户中:
管理控制台->安全->高级设置->管理 API 客户端访问(在身份验证下)
*注意:如果您使用其他范围,则需要键入所有范围,因为它会替换您以前的设置。

更新:
我以为它正在工作,但我可能已经注释掉或忽略了一行。当我当天晚些时候尝试我的代码时,所有行都正确执行,它仍然给我一个 gdata.client.Unauthorized 错误。我尝试重新启动服务器,以便再次创建凭据,但没有帮助。当我尝试进行更新转发调用时发生错误。
我确认 access_token 与用于我的 Directory API 调用的 access_token 相同,并且“客户端”实际上是一个 emailSettingsClient 对象。
我收到的完整错误是:

Unauthorized error message

基于以下内容的另一次尝试:
http://www.worldofchris.com/blog/2012/12/27/fun-with-oauth-gdata-google-apis-client-library-python/
https://groups.google.com/forum/m/#!msg/google-apps-developer-blog/1pGRCivuSUI/3EAIioKp0-wJ How do I authorize a gdata client without using the gdata oauth2 workflow?

credentials = SignedJwtAssertionCredentials(serviceEmail, key, sub=adminEmail, 
    scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/ '+'other scopes')
auth = gdata.gauth.OAuth2Token(serviceEmail, key, 
    scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/',
    access_token=credentials.access_token,
    refresh_token=credentials.refresh_token,
    user_agent='emailsettings/2.0')#I do not really understand this param
client = gdata.apps.emailsettings.client.EmailSettingsClient(domain='mydomain.co')
#Also tried with (domain='mydomain.co', auth_token = credentials.access_token)
client.additional_headers = {'Authorization': 'Bearer %s' % credentials.access_token}
auth.authorize(client)
client.UpdateForwarding(username=username, enable=True, 
    forward_to=forwardTo, action='ARCHIVE')

最佳答案

这应该是直接使用凭据对象的正确方法:

import gdata.gauth

credentials = SignedJwtAssertionCredentials(serviceEmail,
                                            key, 
                                            sub=adminEmail,
                                            scope=scopes)
client = gdata.apps.emailsettings.client.EmailSettingsClient(domain='mydomain.co')
client.auth_token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
client.UpdateForwarding(username=username, enable=True, 
forward_to=forwardTo, action='ARCHIVE')

关于python - 使用 Google Apps 服务帐户启用邮件转发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29609674/

相关文章:

python - 如何使用 Pandas DataFrame 读取包含图像像素值的 csv 文件

python - Unicode SendKeys 替代方案(任何编程语言)

ios - Google YouTube API v3 iOS 应用 key ,403 错误代码

google-oauth - 是否可以从同一个 Google Apps 域中获取所有用户?

php - Google Groups Directory API - 将用户添加到组引发错误 - PHP

python - 如何在任何行在 Python pandas 数据框中具有 NaN 值后删除列

Python:将灰度图像转换为 RGB

javascript - Calendar.events.lists 回调参数不会被调用

oauth - 如何更改 google API "service account"名称/电子邮件地址

java - Google 应用引擎更新用户信息时出现错误 400 BAD_REQUEST