在Team Drive和Colab中创建新的电子表格时,Gspread文件未找到错误。

问题描述 投票:0回答:1

我在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中找到任何与团队驱动相关的参数。

有什么线索吗?

python google-colaboratory gspread google-drive-team-drive
1个回答
0
投票

这是不优雅的,但你可以使用驱动器的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()
© www.soinside.com 2019 - 2024. All rights reserved.