How to use credential from JSON file to use Google drive API in a cloud function

Viewed 25

I've a cloud function in Python that need to access a Google Shared Drive. I've get a JSON file with the credential of the account (can't be a service account because the shared drive required user from the domain) with this doc: https://developers.google.com/workspace/guides/create-credentials#desktop-app how I get the JSON

The JSON file looks like:

{"installed":{"client_id":"xxx.apps.googleusercontent.com","project_id":"xxx-my-project","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxx-my-client-secret","redirect_uris":["http://localhost"]}}

In my cloud function, I use this lines:

import google.auth
from google.oauth2.credentials import Credentials
[...]
    creds = Credentials.from_authorized_user_file('client_secrets.json', scopes=None)
    service = build('drive', 'v3', credentials=creds)

I've put the client_secrets.json file at the same level as my main.py.

At first I've needed to remove: {"installed": and the last } because the error was that client_id etc... parameters were missing.

The error I got now and don't understand why is:

google.auth.exceptions.RefreshError: ('invalid_request: Missing required parameter: refresh_token', {'error': 'invalid_request', 'error_description': 'Missing required parameter: refresh_token'})

Where could I get this value and why is the downloaded JSON file not working without modification ?

I've tried adding: "refresh_token":"" or "refresh_token":None but it was not OK ;-)

Thanks for you help in this.

0 Answers
Related