我无法使用您在下面看到的代码读取我保存在“Google 云端硬盘”上的文件。
一切似乎都很好,代码没有出现错误,但找不到 Google 云端硬盘上的文件,为什么?
我的错在哪里?
谢谢
斯特凡诺·G.
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
service_account_json_key = '****sa-key-jsonfile****.json'
SCOPES = ['https://www.googleapis.com/auth/drive']
creds = service_account.Credentials.from_service_account_file(
filename=service_account_json_key, scopes=SCOPES)
def search_file():
"""Search file in drive location"""
try:
# create drive api client
service = build("drive", "v3", credentials=creds)
files = []
page_token = None
while True:
# pylint: disable=maybe-no-member
response = (
service.files()
.list(
# q="mimeType='image/jpeg'",
spaces="drive",
fields="nextPageToken, files(id, name)",
pageToken=page_token,
)
.execute()
)
for file in response.get("files", []):
# Process change
print(f'Found file: {file.get("name")}, {file.get("id")}')
files.extend(response.get("files", []))
page_token = response.get("nextPageToken", None)
if page_token is None:
break
except HttpError as error:
print(f"An error occurred: {error}")
files = None
return files
if __name__ == "__main__":
ret = search_file()
print(ret)
我终于找到了如何做到这一点: 我必须将包含我的文件的目录共享到服务帐户的电子邮件!