googlearchive / PyDrive

Google Drive API Python wrapper library
Other
1.31k stars 272 forks source link

Script does nothing after authenticating from the browser! #192

Closed thekillgfx closed 4 years ago

thekillgfx commented 4 years ago

What am I looking to do? I would like to develope a script using Python3.6 to check every once in a while which files are located into a Google Drive folder with the folder id given, in return I'd like to receive a list containg all the files ids.

Where am I doing it? I'm working on Repl.it right now, but I'll move to Ubuntu server as soon as the script will work fine.

What I've done since I've started? I'm just at the start of the project since I'm having problems with the authentication to the Google Drive API, I've followed that https://pythonhosted.org/PyDrive/quickstart.html guide. Here you can see my OAuth 2.0 client ID settings -> https://imgur.com/a/S3n5Peh.

After starting the following script

from pydrive.auth import GoogleAuth

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

from pydrive.drive import GoogleDrive
print('authenticaton went good')
drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'title': 'Hello.txt'})
file1.SetContentString('Hello World!')
file1.Upload()

The console output this

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?client_id=614937294403-q1fpjhg93tv82p42h3c5lcbdqkpml9af.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&access_type=offline&response_type=code

then I copy and paste the link into my Chrome browser and go on with the authentication until I got redirected to http://localhost, so I assume the authentication went well, right? But then the script doesn't go on: it seems to be stack on gauth.LocalWebserverAuth() since the console never printed out "authenticaton went good". I assume that is because the browser from where I authenticate my user isn't in the same place of the script, this since I'm running the script on Repl.it but once I've moved to Ubuntu Server the problem would be the same!

How can I solve this problem?

shcheklein commented 4 years ago

@thekillgfx you need to use CommandLineAuth() instead, most likely. LocalWebserverAuth() runs a server (in your case on Repl.it machine) and you should be running browser on the same machine.

thekillgfx commented 4 years ago

@shcheklein Where should I get the verification code the console is asking me?

shcheklein commented 4 years ago

@thekillgfx it should also print a link you need to open in your browser and in your browser you will get the code.

thekillgfx commented 4 years ago

@shcheklein image

shcheklein commented 4 years ago

@thekillgfx when you were creating your app (to get client id, secret) you should have picked type other or command line (not web). I think you can go to the Google Console and change the type.

thekillgfx commented 4 years ago

Thanks, now the authentication is finally working 💯