Using Python to do basic Google Drive operations without web server authentication

I want to create a Python script that can do some really basic things (add/remove files in a shared google drive) The script will be running on remote PCs so anything involving web authentication is off the table

I read online that using a service account was the way to go, so I did the following:

  • Created a project

  • Enabled the Google Drive API for the project

  • Created a service account for the project (left "Grant this service account access to project", couldn't figure out what, if anything, I should put here)

  • Created a key for the service account, downloaded it and tried to use it the following way:

     service_account_info = json.load(open('service_account.json'))
     SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly&#39',
               "https://www.googleapis.com/auth/drive.file",
               "https://www.googleapis.com/auth/drive"]
    
     creds = service_account.Credentials.from_service_account_info(service_account_info, scopes=SCOPES)
    
     service = build('drive', 'v3', credentials=creds)
    
     results = service.files().list(pageSize=10, fields="nextPageToken, files(id, name)").execute()
     items = results.get('files', [])
    

Which results in an exception after my "results" line executes:

google.auth.exceptions.RefreshError: ('No access token in response.', {'id_token': '[longtoken'})

I saw something that hinted I needed to use the "OAuth 2.0 Playground" where I tick off "Use your own Oauth credentials", specify the ClientID and ClientSecret and authorize the Google Drive APIs but when I try to do that I Just get "Error 400: redirect_uri_mismatch" (and I'm not sure if resolving this will actually get me anywhere)

Is there a viable way of doing what I want to do?



Read more here: https://stackoverflow.com/questions/64881151/using-python-to-do-basic-google-drive-operations-without-web-server-authenticati

Content Attribution

This content was originally published by Adam Vengroff at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: