2

我想编辑谷歌应用域中用户的签名。我打算使用服务帐户。服务帐户被委派给整个域。我使用 gmail API 来发送和检索电子邮件,但使用不同的 api 修改了签名。根据https://developers.google.com/admin-sdk/email-settings/这个 api 是我通过 Google Developer Console 启用的 Admin SDK。

我正在尝试使用库 gdata.apps.emailsettings.client (它不支持 Python 3.x)

建立凭据有效,但是当我尝试这样做时

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 Apps 域安全控制面板(管理 API 客户端访问)中,服务 ID 具有“电子邮件设置(读/写) https://apps-apis.google.com/a/feeds/emailsettings/2.0/ ”的权限

4

2 回答 2

3

电子邮件设置 API 要求您以超级管理员身份进行身份验证,普通用户无法访问该 API。因此,您的服务帐户应该充当超级管理员,然后超级管理员为 API 调用中指定的用户进行更改。

于 2016-04-14T10:25:01.063 回答
1

请注意:此答案已过时,并且使用了已弃用的 API。这个要点展示了这样做的新方法(以及在 python3 中) https://gist.github.com/timrichardson/e6ee6640a8b7fe664f3a5a80406ca980


正确的。我希望这篇文章可以节省工作时间。

没有很好地记录的关键缺失点(好吧,一点也不,但也许我错过了)。您需要使用 delegated_credentials 并使用该对象授权 email_settings_client。

  1. 按照 oauth2 说明创建服务帐户并照常下载 JSON 私钥。您可以在开发人员控制台上执行此操作。服务帐户未链接到任何域,它只是一个凭据。
  2. 在您的谷歌应用程序域中,进入安全、高级、api 等。步骤 1 和 2 目前记录在此处:https ://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

现在,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

于 2016-04-16T02:06:40.210 回答