通过 Drive API 在国际设置中创建具有设置语言环境的 Drive 文件

Create a Drive File with a set locale in international settings through Drive API

我需要使用 Google 驱动器 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 密钥与区域设置设置为想要的区域,但它一直使用我帐户的默认区域设置创建文件。

如何让它在创建时起作用?还有其他我可以填写的参数吗?

我刚刚在 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 如果这是您用来检查属性结果的内容。

你的问题是你混淆了两个不同的"properties"。

您正在设置的属性是用户定义的属性,仅供您自己使用。它们对Google没有意义。

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

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