Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 471
user3438152 Avatar asked Apr 28 '26 17:04

user3438152


1 Answers

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)
like image 74
user3438152 Avatar answered Apr 30 '26 06:04

user3438152



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!