How do I authenticate with the google email settings API using the oauth2 Python client service account?

I'm using Python 2.6 and the Google API client library I'm trying to use to get authenticated access to my 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 following error: UnknownApiNameOrVersion: name: email version: v2

What is the api name and version for v2 email settings? Can I use it with a service account? Relations

+3


source to share


3 answers


I found a solution to get email settings using oauth2 service account: Here is an 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)

      

+3


source


It looks like the Email API is not available using the Discovery API. The APIs Discovery service returns information about the API - what methods are available, etc.

See the next question raised in the PHP API.

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



It is not clear why email settings are not available through the discovery API, or whether this is planned. In fact, it seems that many of these systems and libraries remain unchanged.

The legacy gdata client library has support. Try the following example which I can confirm is working fine.

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

0


source


If you have multiple entry points to your application that need to access the EmailSettings API, here's a reusable function that returns a client object:

def google_get_emailsettings_credentials():
    '''
    Google 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 somewhere else, for example. to achieve 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)

      

0


source







All Articles