使用python服务帐户管理gmail签名
我想编辑谷歌应用程序域中用户的签名。 我计划使用一个服务帐户。服务帐户被委派给整个域。 我使用gmail API发送和检索电子邮件,但签名是使用不同的API修改的。 根据这个api,是我通过Google开发者控制台启用的管理SDK 我正在尝试使用图书馆 gdata.apps.emailsettings.client(不支持Python 3.x) 构建凭证是可行的,但当我尝试这样做时使用python服务帐户管理gmail签名,python,google-apps,google-admin-sdk,google-email-settings-api,Python,Google Apps,Google Admin Sdk,Google Email Settings Api,我想编辑谷歌应用程序域中用户的签名。 我计划使用一个服务帐户。服务帐户被委派给整个域。 我使用gmail API发送和检索电子邮件,但签名是使用不同的API修改的。 根据这个api,是我通过Google开发者控制台启用的管理SDK 我正在尝试使用图书馆 gdata.apps.emailsettings.client(不支持Python 3.x) 构建凭证是可行的,但当我尝试这样做时 gmail_client.RetrieveSignature(username) 我明白了 gdata.clie
gmail_client.RetrieveSignature(username)
我明白了
gdata.client.Unauthorized:Unauthorized-服务器响应为:401
# python 2.7 from macports
def setup_gmail_client_new_api():
client_email = '...3@developer.gserviceaccount.com'
key_path = 'VCI-EMAIL-INTEGRATION-f493528321ba.json'
sender = 'tim@vci.com.au'
API_scope = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/'
filename=key_path, scopes=API_scope)
credentials = ServiceAccountCredentials.from_json_keyfile_name(key_path, scopes=API_scope)
return credentials
if __name__ == "__main__":
credentials = setup_gmail_client_new_api()
client = gdata.apps.emailsettings.client.EmailSettingsClient(domain='vci.com.au')
auth = gdata.gauth.OAuth2Token(
credentials.client_id,#serviceEmail
credentials.client_secret,#private key
scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/',
access_token=credentials.access_token,
refresh_token=credentials.refresh_token,
user_agent=credentials.user_agent)
auth.authorize(client)
result = retrieve_sig(client,"tim")
print (result)
检索签名的尝试失败:
gdata.client.Unauthorized: Unauthorized - Server responded with: 401,
服务帐户具有域范围的委派。
在google应用程序域安全控制面板(管理API客户端访问)中,
服务ID具有“电子邮件设置(读/写)”权限。电子邮件设置API要求您以超级管理员身份进行身份验证,普通用户无法访问该API。因此,您的服务帐户应该充当超级管理员,然后再由超级管理员为API调用中指定的用户进行更改。请注意:此答案已过时,并且使用了不推荐使用的API。 这个要点展示了新的方法(在python3中)
对。我希望这篇文章能节省工作时间 关键遗漏点没有很好地记录(好的,一点也没有,但可能我遗漏了)。 您需要使用委派的\u凭据并授权使用该对象的电子邮件\u设置\u客户端
from __future__ import print_function
import httplib2
from oauth2client.service_account import ServiceAccountCredentials
import gdata.apps.emailsettings.client
import gdata.gauth
def setup_credentials():
key_path = '/Users/tim/Dropbox/pycharm_projects_27/gmail_admin/<blabla>.json'
API_scopes =['https://apps-apis.google.com/a/feeds/emailsettings/2.0/']
credentials = ServiceAccountCredentials.from_json_keyfile_name(key_path, scopes=API_scopes)
return credentials
if __name__ == "__main__":
credentials = setup_credentials()
# pass the email address of a domain super-administrator
delegated_credentials = credentials.create_delegated('tim@vci.com.au')
http = httplib2.Http()
#http = credentials.authorize(http)
http = delegated_credentials.authorize(http) #this is necessary
url_get_sig = 'https://apps-apis.google.com/a/feeds/emailsettings/2.0/vci.com.au/tim/signature'
r = http.request(url_get_sig,"GET")
print (r)
# now use the library
client = gdata.apps.emailsettings.client.EmailSettingsClient(domain='vci.com.au')
auth2token = gdata.gauth.OAuth2TokenFromCredentials(delegated_credentials)
auth2token.authorize(client)
r = client.retrieve_signature('tim')
print (client)
from\uuuuu future\uuuuu导入打印功能
导入httplib2
从oauth2client.service\u帐户导入ServiceAccountCredentials
导入gdata.apps.emailsettings.client
导入gdata.gauth
def设置_凭据():
key_path='/Users/tim/Dropbox/pycharm\u projects\u 27/gmail\u admin/.json'
API_作用域=['https://apps-apis.google.com/a/feeds/emailsettings/2.0/']
credentials=ServiceAccountCredentials.from_json_keyfile_name(key_path,scopes=API_scopes)
返回凭证
如果名称=“\uuuuu main\uuuuuuuu”:
凭据=设置\u凭据()
#传递域超级管理员的电子邮件地址
委托的\u凭证=凭证。创建\u委托凭证('tim@vci.com.au')
http=httplib2.http()
#http=凭据。授权(http)
http=委托的\u凭据。授权(http)#这是必需的
url\u get\u sig='1https://apps-apis.google.com/a/feeds/emailsettings/2.0/vci.com.au/tim/signature'
r=http.request(url\u get\u sig,“get”)
印刷品(r)
#现在使用图书馆
client=gdata.apps.emailsettings.client.EmailSettingsClient(domain='vci.com.au')
auth2token=gdata.gauth.OAuth2TokenFromCredentials(委托的\u凭证)
auth2token.authorize(客户端)
r=客户端。检索\u签名(“tim”)
打印(客户端)
这是一个更丰富的示例:用户tim@vci.com.au是此域上的超级管理员。在该过程中,我在何处作为用户进行身份验证?我认为ClientLogin不受欢迎。那么这仍然正确吗?:client.ClientLogin(email=)adminUsername@yourdomain“,password='adminPassword',source='your-apps')source='your-apps'中有什么内容?根据您的回答,我尝试了另一种方法:client.RetrieveSiganture('tim'))现在的原因:令牌无效-无效令牌:无法分析引用的令牌字符串:base64令牌上的无效gaia_data.AuthSubToken proto:6�� 错误401