使用python服务帐户管理Gmail签名 [英] Manage gmail signatures with python service account

查看:144
本文介绍了使用python服务帐户管理Gmail签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编辑google apps域中用户的签名. 我计划使用服务帐户.服务帐户被委派到整个域. 我使用gmail API来发送和检索电子邮件,但是使用其他api修改了签名. 根据 https://developers.google.com/admin-sdk/email-settings/此api是我已通过Google Developer Console启用的Admin SDK.

I want to edit signatures of users in a google apps domain. I plan to use a service account. The service account is delegated to the entire domain. I have this working with the gmail API to send and retrieve email, but signatures are modified using a different api. According to https://developers.google.com/admin-sdk/email-settings/ this api is the Admin SDK which I have enabled via Google Developer Console.

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

I am trying to use the library gdata.apps.emailsettings.client (which doesn't have Python 3.x support)

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

Building the credentials works, but when I try to do

gmail_client.RetrieveSignature(username)

我明白了 gdata.client.Unauthorized:未经授权-服务器回应:401.

I get gdata.client.Unauthorized: Unauthorized - Server responded with: 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/"

the service account has domain-wide delegation. In the google apps domain security control panel (Manage API client access), the service ID has permission for "Email Settings (Read/Write) https://apps-apis.google.com/a/feeds/emailsettings/2.0/"

推荐答案

请注意:此答案已过期,并使用了已弃用的API. 该要点显示了执行此操作的新方法(以及在python3中) https://gist.github.com/timrichardson/e6ee6640a8b7fe664f3a5a80406ca980

Please note: this answer is out of date and uses a deprecated API. This gist shows the new way of doing it (and in python3) https://gist.github.com/timrichardson/e6ee6640a8b7fe664f3a5a80406ca980

对.我希望这篇文章可以节省工作时间.

Right. I hope this post saves hours of work.

关键的遗漏点并没有得到很好的记录(好的,一点也没有,但也许我错过了). 您需要使用proxyed_credentials并为该对象的email_settings_client授权.

Key missing point which was not documented very well (ok, not at all, but perhaps I missed it). You need to use delegated_credentials and authorise the email_settings_client with that object.

  1. 按照oauth2注释创建服务帐户,并照常下载JSON私钥.您可以在开发者控制台上执行此操作.该服务帐户未链接到任何域,而只是一个凭据.
  2. 在您的Google Apps域中,进入安全性,高级,API等. 步骤1和当前在此处记录了2个: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
  1. Follow the oauth2 notes to make a service account and download a JSON private key as usual. You do that on the developer console. The service account is not linked to any domain, it is just a credential.
  2. In your google apps domain, go the security, advanced, api etc etc. Steps 1 & 2 are currently documented here: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

现在,是python代码.我使用2.7是因为我认为gdata不兼容v3.

Now, the python code. I used 2.7 because I think gdata is not v3 compatible.

这是一个最小的示例.

This is a minimal example.

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

this is a richer example here: https://gist.github.com/timrichardson/a43462aedc0797ecb76c48deb9c96d36

这篇关于使用python服务帐户管理Gmail签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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