我正在尝试更新 Google 表格表格中的单元格。我确信工作表 ID、凭据、表中的页面(“名称”)和单元格(“B2”)都是正确的。
堆栈跟踪:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found
PUT https://sheets.googleapis.com/v4/spreadsheets/1OF8UZnI8AZ0ExmTxXdq7NmXCQo_8dg4pTV6Z8e4KQKQ/values/%D0%9B%D0%B8%D1%81%D1%821!B2?valueInputOption=RAW
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : "Requested entity was not found.",
"reason" : "notFound"
} ],
"message" : "Requested entity was not found.",
"status" : "NOT_FOUND"
}
代码:
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.List;
public class GoogleSheets {
private static final String APPLICATION_NAME = ".....";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS);
private static final String CREDENTIALS_FILE_PATH = ".......";
private static final String TOKENS_DIRECTORY_PATH = "tokens";
private Sheets sheetsService;
public GoogleSheets() throws Exception {
final com.google.api.client.http.HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
this.sheetsService = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
}
private Credential getCredentials(final com.google.api.client.http.HttpTransport HTTP_TRANSPORT) throws Exception {
InputStream in = new FileInputStream(CREDENTIALS_FILE_PATH);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}
public void updateCellValue(String spreadsheetId, String range, String value) {
try {
ValueRange body = new ValueRange().setValues(
Collections.singletonList(Collections.singletonList(value))
);
sheetsService.spreadsheets().values()
.update(spreadsheetId, range, body)
.setValueInputOption("RAW")
.execute();
System.out.println("Cell updated: " + range + " with value: " + value);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
GoogleSheets service = new GoogleSheets();
service.updateCellValue("1OF8UZnI8AZ0ExmTxXdq7NmXCQo_8dg4pTV6Z8e4KQKQ", "Names!B2", "123");
} catch (Exception e) {
e.printStackTrace();
}
}
}
为什么我会在这里收到 404 响应?
您遇到此错误:
{
"code" : 404,
"errors" : [ {
"domain" : "global",
"message" : "Requested entity was not found.",
"reason" : "notFound"
} ],
"message" : "Requested entity was not found.",
"status" : "NOT_FOUND"
}
您在“全局”级别可能遇到的错误包括:
404
- 如果电子表格 ID 无法识别实际的电子表格403
- 如果电子表格 ID 标识实际的电子表格,但您没有权限400
- 如果电子表格Id标识的是实际电子表格,但工作表名称错误,以及其他原因否则你的代码看起来是正确的。