3

我需要使用 Google Drive API 创建一个文件(我正在使用 v3,目前是最新的)。如果重要,请使用 python。

我的代码如下,

drive_service.files().create(supportsTeamDrives=True, body={
                    'name': 'test-file',
                    'mimeType': 'application/vnd.google-apps.spreadsheet',
                    'parents': [folder_id],
                    'properties': {'locale': 'en_GB',
                                   'timeZone': 'Europe/Berlin'}
                })

根据@ here的文档,我尝试将properties密钥设置为所需的区域设置,但它会继续使用我帐户的默认区域设置创建文件。

我怎样才能让它在创建时工作?我可以填写另一个参数吗?

4

2 回答 2

3

你的问题是你混合了两个不同的“属性”。

您设置的属性是用户定义的属性,仅由您自己使用。它们对谷歌没有意义。

您要设置的属性是电子表格 API 的一部分。请参阅https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#SpreadsheetProperties

最简单的解决方案是不使用 Drive API 创建电子表格。而是使用电子表格 API 作为描述https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/create

于 2019-01-08T15:45:05.180 回答
1

我刚刚在APIs Explorer中测试了这个

创建文件请求

POST https://www.googleapis.com/drive/v3/files?key={YOUR_API_KEY}

{
 "properties": {
  "test": "test"
 },
 "name": "Hello"
}

回复

{    

 "kind": "drive#file",
 "id": "1CYFI5rootSO5cndBD2gFb1n8SVvJ7_jo",
 "name": "Hello",
 "mimeType": "application/octet-stream"
}

文件获取请求

GET https://www.googleapis.com/drive/v3/files/1CYFI5rootSO5cndBD2gFb1n8SVvJ7_jo?fields=*&key={YOUR_API_KEY}

回复

 "kind": "drive#file",
 "id": "1CYFI5rootSO5cndBD2gFb1n8SVvJ7_jo",
 "name": "Hello",
 "mimeType": "application/octet-stream",
 "starred": false,
 "trashed": false,
 "explicitlyTrashed": false,
 "parents": [
  "0AJpJkOVaKccEUk9PVA"
 ],
 "properties": {
  "test": "test"
 },

它似乎工作得很好,我建议您尝试检查以下内容:

  • 在创建文件的响应中返回的文件 ID。以确保您正在检查刚刚上传的那个。每次运行时,您都不会创建一个新文件。
  • 如果这是您用来检查属性结果的方法,fields=*还请记住添加。file.get
于 2019-01-08T13:45:16.190 回答