How to create an OAuth Credentials Google API object from an alternate source

I work with this simple Google API example:

import httplib2 from apiclient.discovery import build from oauth2client.client import flow_from_clientsecrets from oauth2client.file import Storage from oauth2client.tools import run # Path to the client_secret.json file downloaded from the Developer Console CLIENT_SECRET_FILE = 'client_secret.json' # Check https://developers.google.com/gmail/api/auth/scopes for all available scopes OAUTH_SCOPE = 'https://www.googleapis.com/auth/gmail.readonly' # Location of the credentials storage file STORAGE = Storage('gmail.storage') # Start the OAuth flow to retrieve credentials flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, scope=OAUTH_SCOPE) http = httplib2.Http() # Try to retrieve credentials from storage or run the flow to generate them credentials = STORAGE.get() if credentials is None or credentials.invalid: credentials = run(flow, STORAGE, http=http) # Authorize the httplib2.Http object with our credentials http = credentials.authorize(http) # Build the Gmail service from discovery gmail_service = build('gmail', 'v1', http=http) 

And, having seen that I already went through the OAuth stream earlier (in another application other than Python), and have update tokens, etc., I would like to skip the first part of this example and manually create the expected storage file gmail.storage or Create a credential object in another way.

The problem is that I cannot find documentation about the expected format of this storage file or what should be in it, or how to instantiate the credential object in any other way. Sorry I canโ€™t show any work here, but Iโ€™m at a loss. Any point in the right direction would be greatly appreciated.

+6
source share
3 answers

Very simple, obviously, this works:

 from oauth2client.client import GoogleCredentials from oauth2client import GOOGLE_TOKEN_URI access_token = None token_expiry = None token_uri = GOOGLE_TOKEN_URI user_agent = 'Python client library' revoke_uri = None gCreds = GoogleCredentials( access_token, client_id, client_secret, refresh_token, token_expiry, token_uri, user_agent, revoke_uri=revoke_uri ) 
+8
source

You may be interested in oauth2client.file.Storage lib:

 from oauth2client.file import Storage storage = Storage('gmail.storage') credentials = storage.get() storage.put(credentials) 
0
source

As explained here: on the Google Cloud Platform github

You can also use the string to configure it. Specially json string

 import json import os from google.oauth2 import service_account from google.cloud import translate info = json.loads(os.environ['GOOGLE_APPLICATION_CREDENTIALS_JSON_STRING']) creds = service_account.Credentials.from_service_account_info(info) # Instantiates a client translate_client = translate.Client(credentials=creds) 

Note that I used the Google Translate API for this example, but this is the same logic.

There is a bit more explanation on this git question: https://github.com/GoogleCloudPlatform/google-cloud-python/issues/4477

0
source

Source: https://habr.com/ru/post/977885/


All Articles