【问题标题】:I can see only files created with my script pydrive我只能看到用我的脚本 pydrive 创建的文件
【发布时间】:2021-04-01 10:02:52
【问题描述】:

正如标题所说,我对 pydrive 有疑问。我运行了 pydrive 快速入门 (https://googleworkspace.github.io/PyDrive/docs/build/html/quickstart.html) 中给出的代码,并创建了一个设置和凭据文件,以避免一直输入我的凭据。 但是当我运行这段代码时:

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

# Rename the downloaded JSON file to client_secrets.json
# The client_secrets.json file needs to be in the same directory as the script.
gauth = GoogleAuth()
drive = GoogleDrive(gauth)

# List files in Google Drive
fileList = drive.ListFile().GetList()
for drive_file in fileList:
    print('title: %s, id: %s' % (drive_file['title'], drive_file['id']))

我只能看到使用我的脚本创建的文件。例如,如果我在列表文件之前添加这个:

folder = drive.ListFile({'q': "title = 'Python_test' and trashed=false"}).GetList()[0] # get the folder we just created

file = drive.CreateFile({'title': "test.txt", 'parents': [{'id': folder['id']}]})
file.Upload()

我只看到我刚刚创建的文件夹和文件 ID...如果我在我的驱动器上手动添加一个文件(例如在我的浏览器上),它不会出现。

有人知道发生了什么吗?

【问题讨论】:

  • 您有 GoogleAuth 的代码吗?它要求什么范围?
  • @DaImTo 哦,这只是一个范围请求问题,谢谢! (我在设置文件中添加了错误的 oauth_scope)

标签: python google-drive-api pydrive


【解决方案1】:

我刚刚发现问题,在我的settings.yaml文件中,我只添加了这个oauth_scope:

oauth_scope:
  - https://www.googleapis.com/auth/drive.file

但这只能访问应用程序创建的文件。为了纠正这个问题,我需要像这样删除 .file

oauth_scope:
  - https://www.googleapis.com/auth/drive

如果您想了解有关不同范围的更多详细信息,请查看此链接: https://developers.google.com/identity/protocols/oauth2/scopes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多