PyDrive
PyDrive copied to clipboard
PyDrive and crontab
Hello!
I have some issue with crontab jobs and PyDrive. There is the situation.
I have a python script, that creates file and I want PyDrive to upload this file with cronjob once a day. When I execute the script manually with console, I don't have a problem. But when I put the script on crontab, I get the error 'Error opening file', 'client_secrets.json', 'No such file or directory'.
Any advice? Thanks!
It seemed that the the python script cannot find the json file.
Place that file inside the same folder where the scripts is located.
I am having the same problem. Did you mamaged to solve it? Prompt reply is much appreciated
I have solved the problem ,but I can not remember how to solve.This is my code: First ,I maybe create a certification file from Google's certification center API,and download the client_secrets.json. {"web":{"client_id":"235554121-dkulfemldfdafgaffv1fdafdfa.apps.googleusercontent.com","project_id":"modis-235801","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":"yC-TsfsYaafafdazF_6IIFqKi_jP7","redirect_uris":["http://localhost:8080/"],"javascript_origins":["http://localhost:8080"]}}
This is my code: ``` crdPath = os.path.abspath(os.path.dirname(os.getcwd())) crdName = os.path.join(crdPath, "Config", "client_secrets.json")
_g_login = GoogleAuth()
_g_login.LoadClientConfigFile(crdName)
# _g_login.LocalWebserverAuth()
_g_login.LocalWebserverAuth()
self._drive = GoogleDrive(_g_login)
Sorry,I can not remeber the details.
I suggest you read the help of pyDrive carefully and learn how the oauth2 worked.
This website may help
https://console.developers.google.com/projectselector2/apis/api/drive.googleapis.com/overview
none of the link you provided seems to be working for me. os.path.abspath('client_secrets.json) provides me path /home/pi/client_secrets.json
Anyhow, I have downloaded and saved the client_secrets.json file in same location as my project i am working on default directory /home/pi/client_secrets.json /home/pi/something.py this something.py uploads my file "document.txt" to google drive using pydrive.
When I run from Terminal or IDE, it auto authenticates with the local server and file is uploaded successfully. However, When I am using crontab it fails to locate client_secrets.json as the error describes in this thread.
Before changing anything in auth.py, my crontab line was 0 * * * * python /home/pi/something.py >/home/pi/logs/cronlogs 2>&1
at the time of happening, I check my log file and it provides the above mentioned error.. failed to locate client_secrets.json..... as well as all the print lines from the file in the beginning. e.g. line 1 of the file was 'print("Begin Upload")' It displayed this lina and the error below.
than I changed auth.py, i gave full path to it where it is accessing client_secrets.json. i.e. DEFAULT_SETTINGS=['client_config_file]: '/home/pi/client_secrets.json'
I ran the same crontab line as above and my log file was empty. Nothing was printed. and no file was uploaded. No web browser opened as well.
now my crontab line is 0 * * * * cd/home.pi/ && python /home/pi/something.py >/home/pi/logs/cronlog 2>&1
now the log file only show.
"Begin Upload" and nothing after this. no file uploaded no browser opened........... i hope you understand what I tried to described step by step and hoping if you know this solution.
Sorry,I don't know the solution.I suggest you check the client_secrets.json file path.And check the client_screts.json ,such as "redirect_uris":["http://localhost:8080/"],"javascript_origins":["http://localhost:8080"].I am not sure wether it works.
I use automatic and custom authentication with settings.yaml (https://pythonhosted.org/PyDrive/oauth.html#automatic-and-custom-authentication-with-settings-yaml) and was having the same problem.
I solved the problem as follows. See the code: gauth = GoogleAuth(settings_file='absolute path to settings.yaml')
In the settings.yaml file I changed save_credentials_file to absolute path to 'client_secrets.json' file, in my case it's called 'credentials.json'.