如何使用服务帐户oauth2 Python客户端使用Google Email Settings API进行身份验证?

时间:2014-08-04 15:52:51

标签: python oauth-2.0 google-email-settings-api

我正在使用Python 2.6和Google API的客户端库,我尝试使用它来获取对电子邮件设置的身份验证访问权限:

f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
key = f.read()
f.close()
credentials = client.SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,      scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/', sub=user_email)

http = httplib2.Http()
http = credentials.authorize(http)
return discovery.build('email-settings', 'v2', http=http)

当我执行此代码时,我遇到了以下错误: UnknownApiNameOrVersion:name:email-settings version:v2

电子邮件设置V2的api名称和版本是什么? 是否可以将其与服务帐户一起使用? 此致

3 个答案:

答案 0 :(得分:3)

我找到了使用服务帐户oauth2获取电子邮件设置的解决方案: 这是一个例子:

  SERVICE_ACCOUNT_EMAIL = ''
  SERVICE_ACCOUNT_PKCS12_FILE_PATH = ''
  EMAIL_SETTING_URI = "https://apps-apis.google.com/a/feeds/emailsettings/2.0/%s/%s/%s" 

 def fctEmailSettings():

    user_email = "user@mail.com"
    f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
    key = f.read()
    f.close()
    credentials = client.SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key, scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/', sub=user_email)
    auth2token = OAuth2TokenFromCredentials(credentials)
    ESclient = EmailSettingsClient(domain='doamin.com')
    auth2token.authorize(ESclient)
    username = 'username'
    setting='forwarding'
    uri = ESclient.MakeEmailSettingsUri(username, setting)
    entry = ESclient.get_entry(uri = uri,  desired_class = GS.gdata.apps.emailsettings.data.EmailSettingsEntry)

答案 1 :(得分:0)

似乎使用Discovery API无法使用emailsettings API。 API Discovery服务返回API的详细信息 - 可用的方法等。

请参阅PHP客户端API上提出的以下问题

https://github.com/google/google-api-php-client/issues/246

我不清楚为什么通过发现API无法提供电子邮件设置,或者是否有计划这样做。真的感觉很多这些系统和库都没有维护。

已弃用的gdata客户端库确实有支持。请尝试以下示例,我可以确认它可以正常工作。

https://code.google.com/p/gdata-python-client/source/browse/samples/apps/emailsettings_example.py

答案 2 :(得分:0)

如果您的应用中有多个需要访问EmailSettings API的入口点,这里有一个可重复使用的功能,可以返回"客户端"对象:

def google_get_emailsettings_credentials():
    '''
    Google's EmailSettings API is not yet service-based, so delegation data
    has to be accessed differently from our other Google functions.
    TODO: Refactor when API is updated.
    '''

    with open(settings.GOOGLE_PATH_TO_KEYFILE) as f:
        private_key = f.read()

    client = EmailSettingsClient(domain='example.com')
    credentials = SignedJwtAssertionCredentials(
        settings.GOOGLE_CLIENT_EMAIL,
        private_key,
        scope='https://apps-apis.google.com/a/feeds/emailsettings/2.0/',
        sub=settings.GOOGLE_SUB_USER)
    auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
    auth2token.authorize(client)

    return client

然后可以从其他地方调用它,例如到达DelegationFeed:

client = google_get_emailsettings_credentials()
uri = client.MakeEmailSettingsUri(username, 'delegation')
delegates_xml = client.get_entry(
        uri=uri,
        desired_class=gdata.apps.emailsettings.data.EmailSettingsDelegationFeed)