Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

I am trying to update a cell on google sheet using Spreadsheets.Values.Update method. 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:

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

{
  "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.

/**
 * 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();
        }
}
}
like image 836
min wu Avatar asked Sep 17 '17 04:09

min wu


3 Answers

I just figured it out that the "requested entity not found" error was caused by the invalid spreadsheetId. 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

like image 106
min wu Avatar answered Nov 03 '22 19:11

min wu


Please make sure you are using the valid spreadsheet id and a valid scope for accessing spreadsheet. You can refer the valid scopes from google documentation: 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.

like image 28
Youdhveer Avatar answered Nov 03 '22 20:11

Youdhveer


In my case, I don't know where the spreadsheet id is Then I realized it's in its URL enter image description here

like image 31
Abdelrahman Tareq Avatar answered Nov 03 '22 19:11

Abdelrahman Tareq