Last Updated: March 16, 2018
· 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:


# Build credential storage
credStore = DictStore(some_dict)

# check for credential existence
creds = credStore.get()
if (not creds):
     # Create an authentication flow
     authFlow = OAuth2WebServerFlow(
     creds = run_auth(authFlow, credStore)

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

# Build a Google service client
service ='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 ·