简体   繁体   English

Google Sheets API v4 Spreadsheets.Values.Update 返回错误 404:未找到请求的实体

[英]Google Sheets API v4 Spreadsheets.Values.Update returns ERROR 404:Requested entity was not found

I am trying to update a cell on google sheet using Spreadsheets.Values.Update method.我正在尝试使用 Spreadsheets.Values.Update 方法更新谷歌工作表上的单元格。 I am able to access to the google sheet and read data back, but when using Spreadsheets.Values.Update method, I got the following error:我能够访问谷歌工作表并读回数据,但是在使用 Spreadsheets.Values.Update 方法时,出现以下错误:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 Not Found com.google.api.client.googleapis.json.GoogleJsonResponseException: 404 未找到

{
  "code" : 404,
  "errors" : [ {
    "domain" : "global",
    "message" : "Requested entity was not found.",
    "reason" : "notFound"
  } ],
  "message" : "Requested entity was not found.",
  "status" : "NOT_FOUND"
}

below is the code.下面是代码。 I used the quickstart code from google developers guide to create authentication.我使用了 google 开发人员指南中的快速入门代码来创建身份验证。

/**
 * Application name.
 */
public class Quickstart {

private static final String APPLICATION_NAME =
        "Google Sheets API Java Quickstart";

/**
 * Directory to store user credentials for this application.
 */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
        System.getProperty("user.home"), ".credentials/sheets.googleapis.com-java-quickstart");

/**
 * Global instance of the {@link FileDataStoreFactory}.
 */
private static FileDataStoreFactory DATA_STORE_FACTORY;

/**
 * Global instance of the JSON factory.
 */
private static final JsonFactory JSON_FACTORY =
        JacksonFactory.getDefaultInstance();

/**
 * Global instance of the HTTP transport.
 */
private static HttpTransport HTTP_TRANSPORT;

/**
 * Global instance of the scopes required by this quickstart.
 * <p/>
 * If modifying these scopes, delete your previously saved credentials
 * at ~/.credentials/sheets.googleapis.com-java-quickstart
 */
private static final List<String> SCOPES =
        Arrays.asList(SheetsScopes.SPREADSHEETS);

static {
    try {
        HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
    } catch (Throwable t) {
        t.printStackTrace();
        System.exit(1);
    }
}

private static Sheets service;

/**
 * Creates an authorized Credential object.
 *
 * @return an authorized Credential object.
 * @throws IOException
 */
private static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in =
            Quickstart.class.getResourceAsStream("/client_secret.json");

    GoogleClientSecrets clientSecrets =
            GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT,
                    JSON_FACTORY,
                    clientSecrets,
                    SCOPES)
                    .setDataStoreFactory(DATA_STORE_FACTORY)
                    .setAccessType("offline")
                    .build();

    Credential credential =
            new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
                    .authorize("user");

    System.out.println(
            "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());

    return credential;
}

/**
 * Build and return an authorized Sheets API client service.
 *
 * @return an authorized Sheets API client service
 * @throws IOException
 */
private static Sheets getSheetsService() throws IOException {
    Credential credential = authorize();

    return new Sheets.Builder(HTTP_TRANSPORT,
            JSON_FACTORY,
            credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

private static void updateCell(String spreadsheetId, String range, 
    String newData) throws Exception {
    service = getSheetsService();
    ValueRange aValueRange = new ValueRange();
    aValueRange.setMajorDimension("ROWS");
    aValueRange.setRange(range);
    List<List<Object>> dataArr = new ArrayList<List<Object>>();
    List<Object> cellData = new ArrayList<Object>();
    cellData.add(newData);
    dataArr.add(cellData);
    aValueRange.setValues(dataArr);
    System.out.println("\nNew value range: " + aValueRange);
    service.spreadsheets().values().update(spreadsheetId, range, 
    aValueRange).setValueInputOption("RAW").execute();
}

public static void main(String[] args) throws IOException {
    try {
        String updatedSheetId = "123456";
        String range = "F5:F5";
        updateCell(updatedSheetId, range, "inUse"); 
        } catch (Exception ex) {
        ex.printStackTrace();
        }
}
}

I just figured it out that the "requested entity not found" error was caused by the invalid spreadsheetId.我刚刚发现“未找到请求的实体”错误是由无效的电子表格 ID 引起的。 So I assume that "requested entity not found" error means that the server received the request but could not find resource requested or that it doesn't exist所以我假设“找不到请求的实体”错误意味着服务器收到了请求但找不到请求的资源或它不存在

Please make sure you are using the valid spreadsheet id and a valid scope for accessing spreadsheet.请确保您使用有效的电子表格 ID 和访问电子表格的有效范围。 You can refer the valid scopes from google documentation: https://developers.google.com/sheets/api/guides/authorizing您可以参考谷歌文档中的有效范围: https : //developers.google.com/sheets/api/guides/authorizing

I was also facing similar kind of issue when using drive.file scope but after changing to spreadsheet scope, issue resolved.使用drive.file范围时,我也遇到了类似的问题,但在更改为电子表格范围后,问题解决了。

In my case, I don't know where the spreadsheet id is Then I realized it's in its URL就我而言,我不知道电子表格 ID 在哪里然后我意识到它在它的 URL 中在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM