Move Google EmailSettings API python code from OAuth1 to OAuth2 service account

rlandster

To use the new Google Directory API we created an OAuth2 "service account" (see Using OAuth 2.0 for Server to Server Applications). This is basically a PKCS #12 file. All of our Directory API scripts work fine with this service account.

We also use the EmailSettings API (Developer's Guide Email Settings API) to manage some of our Google account settings. These scripts did not move to the new API format, so we continue to use the old OAuth1 authentication method. This has, until recently, worked fine. However, it appears that Google is no longer supporting OAuth1 authentication.

So, we need to move the EmailSettings scripts from OAuth1 to our OAuth2 service account. We are using the gdata Python libraries (GitHub google/gdata-python-client) to make calls to the EmailSettings API. This is how we currently authenticate to make EmailSettings API calls:

import gdata.apps.emailsettings.service
# self is an EmailSettingsService object (gdata.apps.emailsettings.service)
self.domain = "mydomain.com"
self.source = "my application name"
token = get OAuth1 token string from a file
self.SetOAuthInputParameters(
  gdata.auth.OAuthSignatureMethod.HMAC_SHA1,
  consumer_key    = token.oauth_input_params._consumer.key,
  consumer_secret = token.oauth_input_params._consumer.secret
  )
token.oauth_input_params = self._oauth_input_params
self.SetOAuthToken(token)

Using these Python gdata libraries, how do I authenticate using our OAuth2 service account, i.e., the PKCS #12 file, to use the EmailSettings API?

Jay Lee

There's another SO question which shows you how to do this but uses the slightly newer gdata.apps.emailsettings.client methods.

If you must stay with gdata.apps.emailsettings.service then you can "hack" OAuth 2.0 onto the object with something like:

  1. Build your OAuth 2.0 credentials object as you are already doing for your Admin SDK Directory API calls.

  2. Build your GData client object as you are already doing in lines 1-3 of your code.

  3. Grab the access token from your credentials object and apply it as a header to your client object:

    client.additional_headers = { 'Authorization': u'Bearer %s' % credentials.access_token}

If you get a 401 response (access token expired), repeat 1 and 3 to get and apply a new access token.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Google Drive API Python Service Account Example

From Dev

google service account example returns "Error refreshing the OAuth2 token { “error” : “invalid_grant” }"

From Dev

How to migrate OAuth1 token/secret pairs to OAuth2 access tokens for the Dropbox API?

From Dev

google oauth2 how to get private key for service account

From Dev

Google OAuth2 with Service Account - specifying the user email to act on behalf of

From Dev

Use OAuth2 for authentication + compatibility with google.appengine.api.users service

From Dev

Google API OAuth2, Service Account, "error" : "invalid_grant"

From Dev

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

From Dev

Use oauth2 service account to authenticate to Google API in python

From Dev

Google API access using Service Account oauth2client.client.CryptoUnavailableError: No crypto library available

From Dev

Migration from Google AuthSub to OAuth2

From Dev

Use to Chrome Store Publishing API with OAuth2 Service Account

From Dev

Google OAuth2 Service Account HTTP/REST Authentication

From Dev

POSTMAN, OAuth2 and Google Directory API

From Dev

Python Google Analytics OAuth Service Account and 2LO; "sub" parameter and domain wide delegation

From Dev

Google OAuth2 API Refresh Tokens

From Dev

Forcing a user to choose an account via Google OAuth2

From Dev

Use service account for OAuth2 authentication to gmail

From Dev

OAuth2 Login for Google Calendar API

From Dev

Google OAuth2 with Service Account - specifying the user email to act on behalf of

From Dev

Google OAuth2 api

From Dev

How to Google OAuth2 to upload videos to 2 different YouTube account with a Python script

From Dev

Google oauth1 migration to oauth2: Invalid authorization header

From Dev

Move Google EmailSettings API python code from OAuth1 to OAuth2 service account

From Dev

Cannot refresh token from google api OAuth2

From Dev

OAuth1 on CloudSight (python)

From Dev

OAuth1 - Google Script cannot create Twitter Service

From Dev

Dropbox - Migrating oauth1 to oauth2 using token_from_oauth1

From Dev

I am using google API Oauth2 for google sign-in on my website. How do i get the full name and email id variable from this code into my php code?

Related Related

  1. 1

    Google Drive API Python Service Account Example

  2. 2

    google service account example returns "Error refreshing the OAuth2 token { “error” : “invalid_grant” }"

  3. 3

    How to migrate OAuth1 token/secret pairs to OAuth2 access tokens for the Dropbox API?

  4. 4

    google oauth2 how to get private key for service account

  5. 5

    Google OAuth2 with Service Account - specifying the user email to act on behalf of

  6. 6

    Use OAuth2 for authentication + compatibility with google.appengine.api.users service

  7. 7

    Google API OAuth2, Service Account, "error" : "invalid_grant"

  8. 8

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

  9. 9

    Use oauth2 service account to authenticate to Google API in python

  10. 10

    Google API access using Service Account oauth2client.client.CryptoUnavailableError: No crypto library available

  11. 11

    Migration from Google AuthSub to OAuth2

  12. 12

    Use to Chrome Store Publishing API with OAuth2 Service Account

  13. 13

    Google OAuth2 Service Account HTTP/REST Authentication

  14. 14

    POSTMAN, OAuth2 and Google Directory API

  15. 15

    Python Google Analytics OAuth Service Account and 2LO; "sub" parameter and domain wide delegation

  16. 16

    Google OAuth2 API Refresh Tokens

  17. 17

    Forcing a user to choose an account via Google OAuth2

  18. 18

    Use service account for OAuth2 authentication to gmail

  19. 19

    OAuth2 Login for Google Calendar API

  20. 20

    Google OAuth2 with Service Account - specifying the user email to act on behalf of

  21. 21

    Google OAuth2 api

  22. 22

    How to Google OAuth2 to upload videos to 2 different YouTube account with a Python script

  23. 23

    Google oauth1 migration to oauth2: Invalid authorization header

  24. 24

    Move Google EmailSettings API python code from OAuth1 to OAuth2 service account

  25. 25

    Cannot refresh token from google api OAuth2

  26. 26

    OAuth1 on CloudSight (python)

  27. 27

    OAuth1 - Google Script cannot create Twitter Service

  28. 28

    Dropbox - Migrating oauth1 to oauth2 using token_from_oauth1

  29. 29

    I am using google API Oauth2 for google sign-in on my website. How do i get the full name and email id variable from this code into my php code?

HotTag

Archive