4

Set-up

I'm using PyDrive 2.0 to connect to the Google Drive API.

def connect_google_drive_api():
    
    import os   
        
    # use Gdrive API to access Google Drive
    os.chdir('/Users/my/fol/ders/access_google_drive')
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    
    gauth = GoogleAuth()
    gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script
    drive = GoogleDrive(gauth)
    
    return drive

The working directory /Users/mypath/access_google_drive contains the client_secrets.json, which looks like,

{"web":{"client_id":"xxx","project_id":"invoice-creation-290413","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","redirect_uris":["http://localhost:8080/"],"javascript_origins":["http://localhost:8080"]}}

where I replaced the real client_id and client_secret with xxx.


Issue

When the browser (Safari 14.0) shows Gdrive api link wants to access your Google Account and I click Allow, the process seems to be stuck.

After 20 seconds or so, the following error is shown,

Failed to find "code" in the query parameters of the redirect.
Try command-line authentication
Traceback (most recent call last):

  File "<ipython-input-36-792f41ab7318>", line 1, in <module>
    gauth.LocalWebserverAuth()

  File "/opt/anaconda3/lib/python3.7/site-packages/pydrive2/auth.py", line 125, in _decorated
    code = decoratee(self, *args, **kwargs)

  File "/opt/anaconda3/lib/python3.7/site-packages/pydrive2/auth.py", line 273, in LocalWebserverAuth
    raise AuthenticationError("No code found in redirect")

AuthenticationError: No code found in redirect

How do I solve this?

LucSpan
  • 1,831
  • 6
  • 31
  • 66
  • I've managed to connect using the quickstart.py set-up of Google, and adjusting the code a little. See: https://stackoverflow.com/questions/63956706/google-drive-api-quickstart-py-error-400-redirect-uri-mismatch. But I'd love to hear a solution to connect via PyDrive – LucSpan Sep 19 '20 at 08:18
  • Although I asked at [your previous question](https://stackoverflow.com/q/63940770/7108653), in your situation, is `settings.yaml` used? Or you are directly using `client_secrets.json`? Because unfortunately, I cannot replicate your situation of `AuthenticationError: No code found in redirect`. Or, can I ask you about the flow for replicating your situation? By this, I would like to confirm it. – Tanaike Sep 23 '20 at 23:31
  • I'm not using `settings.yaml`. I have the `client_secrets.json` in the working directory. – LucSpan Sep 24 '20 at 08:51
  • Thank you for replying. I tested your situation using `client_secrets.json` without using `settings.yaml`. But unfortunately, I cannot replicate your situation. In my environment, no error occurs using `client_secrets.json` with `"redirect_uris":["http://localhost:8080/"],"javascript_origins":["http://localhost:8080"]`. So can you provide the detail flow for replicating your issue? By this, I would like to think of the solution. – Tanaike Sep 24 '20 at 22:25
  • @Tanaike, I've edited the code in the question to adjust for the version I use. Namely, I'm calling the connection into another script, say script `x.py`. I had set both the working directory in `x.py` and the code above. It seems that all I needed to do was remove the `os.chdir('/Users/my/fol/ders/access_google_drive')` line from the code above. – LucSpan Sep 25 '20 at 15:03
  • 1
    Thank you for replying. I'm glad your issue was resolved. – Tanaike Sep 25 '20 at 23:45

2 Answers2

1

I was importing connect_google_drive_api() into another script, say script x.py.

Both the connect_google_drive_api() function and x.py had the line os.chdir('/Users/my/fol/ders/access_google_drive') to set the working directory to where the client_secrets.json was.

With the following code, I actually make a connection without any issue,

def connect_google_drive_api():
        
    # use Gdrive API to access Google Drive
    from pydrive2.auth import GoogleAuth
    from pydrive2.drive import GoogleDrive
    
    gauth = GoogleAuth()
    gauth.LocalWebserverAuth() # client_secrets.json need to be in the same directory as the script    
    
    drive = GoogleDrive(gauth)
    
    return drive
LucSpan
  • 1,831
  • 6
  • 31
  • 66
0

Was trying to run this code from a Jupiter notebook. I was able to successfully authenticate when I switched browsers from Safari(14.0.1) to Chrome(87.0.4280.88)

Pratik
  • 1
  • 1