How to authenticate with Google Email Settings API using service account oauth2 Python client?

为君一笑 提交于 2019-12-10 10:17:47

问题


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)

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

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


回答1:


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 = "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)



回答2:


It appears that the emailsettings API is not available using the Discovery API. The APIs Discovery service returns back details of an API - what methods are available, etc.

See the following issue raised on the PHP client API

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

I'm unclear as to why the emailsettings is not available via the discovery API or whether there are plans to do so. Really it feels like a lot of these systems and libraries are unmaintained.

The deprecated gdata client library does have support. Try the following example, which I can confirm works ok.

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




回答3:


In case you have multiple entry points in your app that need to access the EmailSettings API, here's a re-usable function that returns a "client" object:

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

It can then be called from elsewhere, e.g. to reach the 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)


来源:https://stackoverflow.com/questions/25122708/how-to-authenticate-with-google-email-settings-api-using-service-account-oauth2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!