Last Updated: March 16, 2018
·
2.542K
· mattijs

Storing Google API credentials in a Python dict

The credentials for making Google API requests are generally stored in a file. This does not work well when you want to treat or store the credentials in some other way.

I wrote a small Credentials store for Google API requests that stores the obtained credentials in an existing dictionary: https://gist.github.com/4232775

Usage

# Build credential storage
credStore = DictStore(some_dict)

# check for credential existence
creds = credStore.get()
if (not creds):
     # Create an authentication flow
     authFlow = OAuth2WebServerFlow(
         client_id=CLIENT_ID,
         client_secret=CLIENT_SECRET,
         scope="https://www.googleapis.com/auth/calendar",
         user_agent="myapp/v1"
     )
     creds = run_auth(authFlow, credStore)

# Authorize an HTTP connection
http = httplib2.Http()
http = creds.authorize(http=http)

# Build a Google service client
service = apiclient.discovery.build('calendar', 'v3', http=http)

After authentication the dict some_dict is updated with the credential information. This can now be serialized to JSON for example and stored in a database or over the network.

1 Response
Add your response

hi i've tried your code but Dicstore is unknown.... what do i need to import??

over 1 year ago ·