gpt4 book ai didi

python - PyDrive 在 Google Drive 中创建文件夹 - 错误为 AttributeError : 'GoogleDrive' object has no attribute 'files

转载 作者:行者123 更新时间:2023-12-04 13:30:38 24 4
gpt4 key购买 nike

尝试根据日期使用 PyDrive 在现有文件夹中创建文件夹,但我一直收到错误为 “GoogleDrive”对象没有属性"file"
Documentation

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

gauth = GoogleAuth()
gauth.LoadCredentialsFile("token.txt")

if gauth.credentials is None:
gauth.GetFlow()
gauth.flow.params.update({'access_type': 'offline'})
gauth.flow.params.update({'approval_prompt': 'force'})
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
gauth.Refresh()
else:
gauth.Authorize()
gauth.SaveCredentialsFile("token.txt")
drive = GoogleDrive(gauth)

file_metadata = {'name': 'Test','mimeType': 'application/vnd.google-apps.folder'}
file = drive.files().create(body=file_metadata).execute()
print('Folder ID: %s' % file.get('id'))
但我收到错误消息,因为“GoogleDrive”对象没有属性"file"”
有什么我做错了吗??

最佳答案

在我的案例中有效的是使用不同的身份验证方式:

from httplib2 import Http
from googleapiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

SCOPES = ["https://www.googleapis.com/auth/drive"]
PATH_TO_SERVICE_KEYS = "/path/to/service/keys.json"
credentials = ServiceAccountCredentials.from_json_keyfile_name(PATH_TO_SERVICE_KEYS, scopes=SCOPES)
http_auth = credentials.authorize(Http())
drive = build("drive", "v3", http=http_auth)
这一次,它没有出现这个错误。
drive = GoogleDrive(gauth)

file_metadata = {"name": "Test", "mimeType": "application/vnd.google-apps.folder"}
file = drive.files().create(body=file_metadata).execute()

关于python - PyDrive 在 Google Drive 中创建文件夹 - 错误为 AttributeError : 'GoogleDrive' object has no attribute 'files,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65303733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com