4

I made a program using Python and PyDrive. The program captures an image and uploads it to a folder on Google Drive. This is the basic code for my authentication and upload:

gauth = GoogleAuth()
drive = GoogleDrive(gauth)
file_image = drive.CreateFile({"parents":  [{"kind": "drive#fileLink","id": upload_folder_id}]})
file_image.SetContentFile(imageName) #Set the content to the taken image
file_image.Upload() # Upload it

Any time I run the program it wants to authenticate me. Is there any way to remember the authentication?

Erica
  • 2,399
  • 5
  • 26
  • 34
Gabe
  • 624
  • 8
  • 19

1 Answers1

0

In the webinterface of Google Drive, cookies are used to remember authification. I would recommend to read the login information from a config file and login each time you use the script. You may use eval() with that, but remember, eval() is evil :) (If the script is only used by you, it's okay).

Sample:

Create a file called login.conf (or similar) in the same folder your script is in. The put this in it:

{"user": "username", "pass": "password"}

Replace "username" and "pasword" with your real values, but keep the ". In your script, do it like this:

with open("login.conf", "r") as f:
    content = eval(f.read())

username = content["user"]
password = content["pass"]

# Now do login using these two variables

A more secure option would be the usage of ConfigParser (configparser in Python 3.x).

EDIT:

It looks like login to Google Drive requires afile called client_secrets.json in the directory of your script. If you have it, do login like this:

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

gauth.LoadCredentialsFile("credentials.txt")
if gauth.credentials is None:
    gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
    gauth.Refresh()
else:
    gauth.Authorize()

gauth.SaveCredentialsFile("credentials.txt")

drive = GoogleDrive(gauth)

Hope this helps (Ask, if something is not clear enough)!

linusg
  • 6,289
  • 4
  • 28
  • 78
  • Thank you! The other part what is not clear for me is that, how can I use this information to login to my Google Drive. How is it possible with PyDrive? – Gabe Apr 26 '16 at 12:50
  • Ok. I've never used PyDrive, but the docs says, you need a file called `client_secrets.json`. Do you have it? – linusg Apr 26 '16 at 12:58
  • Yes, that's the file where I store my Google Drive api secret keys – Gabe Apr 26 '16 at 13:01
  • Updated my answer, please try. The first code sample is not needed anymore, but useful for later projects using config files :) – linusg Apr 26 '16 at 13:07
  • Yes this is it, but now I need authorization every time I use the python app. (Thanks for the config code, that really helps :) ) – Gabe Apr 26 '16 at 13:10
  • If yes, you might accept my answer. If not, what is the problem now? – linusg Apr 26 '16 at 13:14
  • It's not solved because I have to authorize myself with my browser every time I run the app – Gabe Apr 26 '16 at 13:16
  • Hey, I've found something else: try my edited code! This should require a webbrowser loging only at the first time and when your password was changed! – linusg Apr 26 '16 at 13:20
  • I tried it and it authorizes once than again and than error: `Failed to find "code" in the query parameters of the redirect. Try command-line authentication Traceback (most recent call last): File "testDrive.py", line 10, in gauth.LocalWebserverAuth() File "C:\Python27\lib\site-packages\pydrive\auth.py", line 69, in _decorated code = decoratee(self, *args, **kwargs) File "C:\Python27\lib\site-packages\pydrive\auth.py", line 188, in LocalWebserverAuth raise AuthenticationError('No code found in redirect') pydrive.auth.AuthenticationError: No code found in redirect` – Gabe Apr 26 '16 at 13:25
  • Ok, thanks for reply. I'll install the PyDrive package and do some test. Keep looking, if I'm updating my answer – linusg Apr 26 '16 at 15:23
  • Thank you so much for putting this much effort in it. – Gabe Apr 26 '16 at 22:33