
On the server side: gdrive_updatedb(payload):ĭrive_service = build('drive', 'v3', credentials=creds) Self.label_gdrive.text = "Error connecting to your Google Drive!" Self.label_gdrive.text = "Updated your Google Drive successfully!" If _user(allow_remembered=True):ĭone = ('gdrive_updatedb', db_file) Wrapping it all together in a while loop on the client side, we can quite graciously get access and refresh tokens, refresh the access token using the refresh token and, if everything fails, prompt the user for a new login (we even give the user three tries before failing): done = False Token_uri=token_uri, client_id=client_id, client_secret=client_secret)Ī_user() = creds.tokenĮxcept :Ī_user() = NoneĪnd an example of using the API (in this case to update an existing database file whose id is stored in a datatable). Refresh_token = refresh_token=_user()Ĭlient_id = _secret('google_client_id')Ĭlient_secret = _secret('google_client_secret')Ĭreds = (access_token, # google apisįrom import requestsįrom googleapiclient.discovery import buildįrom googleapiclient.http import gdrive_store_tokens(access_token, refresh_token):Ī_user() = access_tokenĪ_user() = refresh_tokenĪccess_token = _user() On the server side we store the tokens (gdrive_store_tokens) and build some credentials (gdrive_creds). Refresh_token = _user_refresh_token()Ī('gdrive_store_tokens', access_token=access_token, refresh_token=refresh_token) On the client side we can call this function whenever we need new tokens: def gdrive_gettoken():Īccess_token = _user_access_token()

We also store the tokens in the respective user row in the datatable (in my case a linked table). You need to store the client_id and client_secret in both the Google API Drive section (for th) and App Secrets (for credential building, see below). Permissions are obtained in a granular scope specific way (so no more outrageous prompts for full drive access and so on).

It uses the googleapiclient library for python and, while the example pertains to the Drive AppData scope, the logic is easily adaptable to any Google API.

Here’s my implementation until Anvil extends its built in integration.
