If you want to save the Pydrive authentication file in a different directory from the script, write as follows.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.file import Storage
gauth = GoogleAuth(settings_file=f"{Absolute Path of the directory you want to save}/settings.yaml")
gauth.credentials = Storage(f"{Absolute Path of the directory you want to save}/credentials.json").get()
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)
python==3.7.6
pydrive==1.3.1
The basic usage and setting method of Pydrive are omitted because the following sites carefully explain it.
Reference site: Download, upload, delete Google Drive with Python, PyDrive, etc .-- note.nkmk.me
If the Pydrive authentication file is not saved in the same directory where the script is saved (by default), the authentication will be NG.
For example, it is a must to save the authentication files client_secrets.json
, credentials.json
, settings.yaml
in the same directory as the script main.py
as shown below.
/home/hogehoge/
└ hoge_project/
└ src/
├ client_secrets.json
├ credentials.json
├ settings.yaml
└ main.py
main.py
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)
However, it feels awkward to have scripts and authentication files mixed in the src
directory.
So somehow, I want to move the authentication file under the creds /
directory as follows.
/home/hogehoge/
└ hoge_project/
├ creds/
│ ├ client_secrets.json
│ ├ credentials.json
│ └ settings.yaml
│
└ src/
└ main.py
If you write as follows, main.py
can recognize the authentication file undercredits /
.
main.py
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.file import Storage
gauth = GoogleAuth(settings_file=f"/home/hogehoge/creds/settings.yaml")
gauth.credentials = Storage(f"/home/hogehoge/creds/credentials.json").get()
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)
Reference site: [Note that you stumbled on scraping python --Qiita](https://qiita.com/nacopon/items/515194278d0aa875cca0#pydrive-crontab%E3%81%A7%E5%AE%9F%E8%A1 % 8C% E3% 81% 99% E3% 82% 8B% E3% 81% A8% E3% 82% A8% E3% 83% A9% E3% 83% BC)
I had a hard time finding information. Please let me know if there is anything else.
Recommended Posts