[仅使用API密钥即可从工作表中读取数据(不使用OAuth 2.0),但是似乎读取开发人员元数据需要OAuth 2.0。有什么方法可以在不要求用户连接其Google帐户的情况下从应用程序读取元数据吗?
如果我的理解是正确的,那么这个答案怎么样?请认为这只是几个可能的答案之一。
[遗憾的是,Sheets API中的“ REST资源:电子表格.developerMetadata”不能与API密钥一起使用。在这种情况下,如您的问题中所述,需要OAuth2。开发人员元数据也可以通过Sheets API中的sheets.get方法来检索。可以通过API密钥检索开发者元数据。并且在这种方法中,将检索所有开发人员元数据。因此,当您要搜索开发人员元数据时,请从检索到的所有开发人员元数据中进行搜索。
重要点:
在这种情况下,请将开发人员元数据的可见性设置为
DOCUMENT
。这样,可以通过API密钥检索开发人员元数据。如果可见性为PROJECT
,则无法使用API密钥进行检索。请注意这一点。当您想使用API密钥检索开发人员元数据时,请公开共享电子表格。这样,可以使用API密钥检索它。请注意这一点。
作为示例情况,它假设它创建了新的电子表格,并为该电子表格创建了新的开发人员元数据,作为“ sampleKey”的键和“ sampleValue”的值。
在这种情况下,电子表格.batchUpdate的示例请求正文如下。
{
"requests": [
{
"createDeveloperMetadata": {
"developerMetadata": {
"location": {
"spreadsheet": true
},
"metadataKey": "sampleKey",
"metadataValue": "sampleValue",
"visibility": "DOCUMENT"
}
}
}
]
}
当您从上述示例电子表格中检索开发人员元数据时,请使用以下curl命令。
curl "https://sheets.googleapis.com/v4/spreadsheets/### spreadsheetId ###?key=### your API key ###&fields=developerMetadata"
fields=developerMetadata
使查看响应值更加容易。当然,您也可以将*
用作fields
。{
"developerMetadata": [
{
"metadataId": 123456789,
"metadataKey": "sampleKey",
"metadataValue": "sampleValue",
"location": {
"locationType": "SPREADSHEET",
"spreadsheet": true
},
"visibility": "DOCUMENT"
}
]
}
在另一种情况下,它假定创建新的电子表格,并在第一列(列“ A”)上创建新的开发人员元数据,作为“ sampleKey”的键和“ sampleValue”的值。
在这种情况下,样本请求正文如下。
{
"requests": [
{
"createDeveloperMetadata": {
"developerMetadata": {
"location": {
"dimensionRange": {
"sheetId": 0,
"startIndex": 0,
"endIndex": 1,
"dimension": "COLUMNS"
}
},
"metadataKey": "sampleKey",
"metadataValue": "sampleValue",
"visibility": "DOCUMENT"
}
}
}
]
}
当您从上述示例电子表格中检索开发人员元数据时,请使用以下curl命令。
curl "https://sheets.googleapis.com/v4/spreadsheets/### spreadsheetId ###?key=### your API key ###&fields=sheets(data(columnMetadata(developerMetadata)))"
sheets(data(columnMetadata(developerMetadata)))
使查看响应值更加容易。当然,您也可以将*
用作fields
。{
"sheets": [
{
"data": [
{
"columnMetadata": [
{
"developerMetadata": [
{
"metadataId": 123456789,
"metadataKey": "sampleKey",
"metadataValue": "sampleValue",
"location": {
"locationType": "COLUMN",
"dimensionRange": {
"dimension": "COLUMNS",
"startIndex": 0,
"endIndex": 1
}
},
"visibility": "DOCUMENT"
}
]
},
{},
,
,
]
}
]
}
]
}
如果我误解了你的问题,而这不是你想要的方向,我深表歉意。