Creating and storing Google credentials in token.pickle from auth code

Viewed 6191

I am writing a program which uses a local token.pickle file to access the user's Google Calendar.

It runs on a server and what I want is for the user to be able to login through a web interface and then the server creates and stores the token.pickle file, created from the auth code the page sends back to the server.

Where I am at is that my Python backend receives the authorization code but I don't know how to create the token.pickle file from that... I know I need to exchange the code for the user's credentials though and I probably need to use the following snippets:

with open('token.pickle', 'wb') as token:
        pickle.dump(creds, token)

and

# Set path to the Web application client_secret_*.json file you downloaded from the
# Google API Console: https://console.developers.google.com/apis/credentials
CLIENT_SECRET_FILE = '/path/to/client_secret.json'

# Exchange auth code for access token, refresh token, and ID token
credentials = client.credentials_from_clientsecrets_and_code(
    CLIENT_SECRET_FILE,
    ['https://www.googleapis.com/auth/drive.appdata', 'profile', 'email'],
    auth_code)

Thanks, I would greatly appreciate any help.

EDIT:

Okay, so I would like to save some sort of way of getting the credentials or the credentials themselves from the access token, so I can access them from:

service = build('calendar', 'v3', credentials=creds)

Can anybody clarify or give me a way to do this please?

0 Answers
Related