python - PyDrive 在 Google Drive 中创建文件夹 - 错误为 AttributeError:“GoogleDrive”对象没有属性“文件”
问题描述
尝试根据日期使用 PyDrive 在现有文件夹中创建一个文件夹,但我不断收到错误,因为“GoogleDrive”对象没有属性“文件” 文档
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”对象没有属性“文件””
有什么我做错了吗?
解决方案
在我的案例中起作用的是使用不同的身份验证方式:
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()
推荐阅读
- selenium - Selenium (Java):dndScript() 方法未定义
- mysql - 内连接不准确匹配
- android - 如何使用 php 和 mysql 将视频上传到服务器?
- java - spark 3.0.0 中的 CBORFactory NoClassDefFoundError 异常
- jpa - JPA多对多关系:无法插入生成的表
- powershell - 使用 PowerShell 写入输出创建后损坏的 Cmd 脚本
- html - 显示弹性元素的中间
- spring-kafka - MessageListenerContainer 的度量方法未捕获正确的值
- reactjs - prevState 和 ...state 之间的区别
- python - 如何在 django rest 框架中返回当前用户的预订历史记录?