使用pydri在google文档中打开文件

2024-06-02 08:46:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以选择从django querysets生成一个html文件。一切工作正常,当点击相应的按钮,它将被重定向到结果页面。现在我需要这样做,生成的文件打开不是在应用程序,而是在谷歌文档。 为此,我使用pydrive库。根据文档,我为测试创建了文件pydrive.pyclient_secrets.json

pydrive\u gd.py

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)

file1 = drive.CreateFile({'title': 'Hello.txt'})  # Create GoogleDriveFile instance with title 'Hello.txt'.
file1.SetContentString('Hello World!') # Set content of the file from given string.
file1.Upload()

这个测试效果很好,在登录到我的Google帐户后,我收到一条消息

Authentication successful.

下一步,我将尝试将pydrive代码连接到django视图。请注意,views.pyclient_secrets.jsonpydrive_gd.py文件位于同一目录中。 我开始向views.py文件中添加代码,并在添加行gauth.LocalWebserverAuth()

视图.py

#Other import
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()#--->ERROR

我出错了

    raise InvalidConfigError('Invalid client secrets file %s' % error)
pydrive.settings.InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)

如果我只是添加到视图中,pyfrom .pydrive_gd import drive错误是相同的


Tags: 文件frompyimportclientjsondrivefile