【问题标题】:Manage gmail signatures with python service account使用 python 服务帐户管理 gmail 签名
【发布时间】:2016-08-05 11:05:55
【问题描述】:

我想编辑 Google Apps 域中用户的签名。 我打算使用服务帐户。服务帐户被委派给整个域。 我使用 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:未授权 - 服务器响应: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/”的权限

【问题讨论】:

    标签: python google-apps google-admin-sdk google-email-settings-api


    【解决方案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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 用户 tim@vci.com.au 是该域的超级管理员。在此过程中,我在哪里进行用户身份验证?我认为 ClientLogin 已被弃用。那么这仍然正确吗?:client.ClientLogin(email='adminUsername@yourdomain', password='adminPassword', source='your-apps') source='your-apps' 中的内容是什么?
      • 我根据您在此处的回答尝试了不同的方法:stackoverflow.com/a/29612057/401226client.RetrieveSiganture('tim') 现在导致:

        令牌无效 - 令牌无效:无法解析引用的令牌字符串:gaia_data 无效base64 令牌上的 .AuthSubToken 原型:6��

        错误 401

      猜你喜欢
      • 1970-01-01
      • 2020-07-29
      • 2017-04-29
      • 2016-01-21
      • 2014-09-13
      • 2022-01-15
      • 2021-06-22
      • 2014-12-13
      • 1970-01-01
      相关资源
      最近更新 更多