我想在google apps域中编辑用户的签名。 我打算使用服务帐户。服务帐户被委派给整个域。 我使用gmail API来发送和检索电子邮件,但是使用不同的api修改了签名。 根据{{3}},此api是我通过Google Developer Console启用的管理员SDK。
我正在尝试使用该库 gdata.apps.emailsettings.client(没有Python 3.x支持)
构建凭据有效,但是当我尝试
时gmail_client.RetrieveSignature(username)
我明白了 gdata.client.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,
服务帐户具有域范围的委派。 在谷歌应用程序域安全控制面板(管理API客户端访问), 服务ID拥有"电子邮件设置(读/写)https://developers.google.com/admin-sdk/email-settings/"
答案 0 :(得分:3)
电子邮件设置API要求您以超级管理员身份进行身份验证,而普通用户无法访问API。因此,您的服务帐户应该充当超级管理员,然后超级管理员为API调用中指定的用户进行更改。
答案 1 :(得分:1)
请注意:此答案已过期,并使用已弃用的API。 这个要点显示了这样做的新方法(并在python3中) https://gist.github.com/timrichardson/e6ee6640a8b7fe664f3a5a80406ca980
右。我希望这篇文章可以节省数小时的工作量。
关键缺失点没有记录得很好(好吧,根本没有,但也许我错过了它)。 您需要使用delegated_credentials并使用该对象授权email_settings_client。
现在,python代码。我使用2.7因为我认为gdata不兼容v3。
这是一个最小的例子。
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)
这是一个更丰富的例子:https://gist.github.com/timrichardson/a43462aedc0797ecb76c48deb9c96d36