如何使用服务帐户oauth2

如何使用服务帐户oauth2

本文介绍了如何使用服务帐户oauth2 Python客户端通过Google Email Settings API进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Python 2.6 and the client library for Google API which I am trying to use to get authenticated access to email settings :

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:名称:电子邮件设置版本:v2

When I execute this code , I got the follwowing error:UnknownApiNameOrVersion: name: email-settings version: v2

电子邮件设置V2的api名称和版本是什么?可以与服务帐户一起使用吗?问候

What's the api name and version for email settingsV2?Is it possible to use it with service account?Regards

推荐答案

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

I found the solution to get email settings using service account oauth2:Here is a example:

  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 = "[email protected]"
    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)

这篇关于如何使用服务帐户oauth2 Python客户端通过Google Email Settings API进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 20:58