使用 Google Apps 服务帐户启用邮件转发 [英] Enable mail forwarding using a Google Apps Service Account

本文介绍了使用 Google Apps 服务帐户启用邮件转发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

相关信息:
我有一个服务帐户并按照本指南对其进行了适当的授权:
https://groups.google.com/forum/m/#!msg/google-apps-developer-blog/1pGRCivuSUI/3EAIioKp0-wJ如何授权 gdata客户端不使用 gdata oauth2 工作流程?

credentials = SignedJwtAssertionCredentials(serviceEmail, key, sub=adminEmail,scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/'+'其他范围')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')#这个参数我不是很懂客户端 = gdata.apps.emailsettings.client.EmailSettingsClient(domain='mydomain.co')#也试过 (domain='mydomain.co', auth_token = credentials.access_token)client.additional_headers = {'Authorization': 'Bearer %s' % credentials.access_token}auth.authorize(客户端)client.UpdateForwarding(username=username, enable=True,forward_to=forwardTo, action='ARCHIVE')

解决方案

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

导入 gdata.gauth凭证 = SignedJwtAssertionCredentials(serviceEmail,钥匙,子=管理员电子邮件,范围=范围)客户端 = 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')

On April 20, 2015, several Google Apps APIs are being discontinued, including the Provisioning API(gdata).
In my Python scripts, I am using a Service Account and OAuth 2.0, instead of ClientLogin, and the replacement API: Directory API.
However, I am unable to find a way to enable mail forwarding using the new API, and all of Google's Python documentation for mail forwarding explains how to do it using ClientLogin, which is also being discontinued on April 20th.

Relevant Info:
I have a service account and authorized it appropriately following this guide: https://developers.google.com/api-client-library/python/auth/service-accounts
All of my other functionality is working with the new API!
I have thoroughly searched the Directory API documentation(though I don't rule out the chance that I missed something): https://developers.google.com/resources/api-libraries/documentation/admin/directory_v1/python/latest/index.html
Google's only documentation(that I found) about implementing mail forwarding with Python suggests using ClientLogin as mentioned above: https://developers.google.com/admin-sdk/email-settings/#manage_forwarding_settings

My existing working code for mail forwarding(based on that documentation):

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')

My updated code based on Jay Lee's answer:

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')

And I added the new scope to my service account in:
Admin Console->Security->Advanced Settings->Manage API Client Access(under Authentication)
*Note: If you are using other scopes, you need to type all of them because it replaces your previous settings.

Update:
I thought I had it was working, but I may have had a line commented out or ignored. When I tried my code later in the day, with everything line being executed properly, it was still giving me a gdata.client.Unauthorized error. I have tried restarting my server so the credentials would be created again, but it has not helped. The error occurs when I try to make the update forwarding call.
I confirmed that the access_token is the same as the one that is working for my Directory API calls, and that "client" is in fact a emailSettingsClient object.
The full error I receive is:

Another attempt based on the following:
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')

解决方案

This should be the proper way to use the credentials object directly:

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')

这篇关于使用 Google Apps 服务帐户启用邮件转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆