[英]Gspread File not found error when creating a new spreadsheet in Team Drive and Colab
我在 Google Colab 工作,试图处理一些数据并将其作为 Google 电子表格保存到我管理的团队驱动器中。 我正在使用 gspread 3.6.0。
我试图保存的文件夹已经存在(我自己创建了它)。 这是我的测试代码:
#Authentication
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
test_df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
folder_id = '1piOfOFFw60hh5SbC9gIPAveCk90thI9_'
sh = gc.create('test_df', folder_id=folder_id)
gspread 抛出的错误如下:
---------------------------------------------------------------------------
APIError Traceback (most recent call last)
<ipython-input-38-a6c430392153> in <module>()
2 folder_id = '1L_ZYzDHi59yHNo2F0ZF8BnQCWJQEu9zR'
3
----> 4 sh = gc.create('df_prueba', folder_id=folder_id)
1 frames
/usr/local/lib/python3.6/dist-packages/gspread/client.py in create(self, title, folder_id)
194 payload['parents'] = [folder_id]
195
--> 196 r = self.request('post', DRIVE_FILES_API_V3_URL, json=payload)
197 spreadsheet_id = r.json()['id']
198 return self.open_by_key(spreadsheet_id)
/usr/local/lib/python3.6/dist-packages/gspread/client.py in request(self, method, endpoint, params, data, json, files, headers)
71 return response
72 else:
---> 73 raise APIError(response)
74
75 def list_spreadsheet_files(self, title=None):
APIError: {'errors': [{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: 1L_ZYzDHi59yHNo2F0ZF8BnQCWJQEu9zR.', 'locationType': 'parameter', 'location': 'fileId'}], 'code': 404, 'message': 'File not found: 1L_ZYzDHi59yHNo2F0ZF8BnQCWJQEu9zR.'}
我从团队驱动器中读取文件没有问题。
如果我将 folder_id 更改为我的驱动器中的文件夹,则代码有效。 我检查了 gspread 文档,但在 API 中没有找到任何与团队驱动器特别相关的参数。
有什么线索吗?
它并不优雅,但您可以使用 Drive API。 试试下面的代码。
from googleapiclient.discovery import build
folder_id = "1piOfOFFw60hh5SbC9gIPAveCk90thI9_"
title = "test_df"
body = {
"name": title,
"mimeType": "application/vnd.google-apps.spreadsheet",
"parents": [folder_id]
}
service = build("drive", "v3", credentials=CREDENTIALS)
service.files().create(body=body, fields="id", supportsAllDrives=True).execute()
我有这个确切的问题。 我们能够通过更新 gspread pip install -U gspread
来解决它。 (我们从 3.6.0 升级到 4.0.1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.