Encountering a 404 ERROR with Google Sheets API v4 when using Spreadsheets.Values.Update method

I’m attempting to modify a specific cell in a Google Sheet by utilizing the Spreadsheets.Values.Update function. Although I can access the sheet and retrieve data successfully, I encounter the following error when I try to execute the Spreadsheets.Values.Update method:

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"
}

I’ve used the sample authentication code provided in the Google Developers guide. Below is an example of my code:

public class MyApplication {

    private static final String APP_NAME = "Google Sheets API Application";
    private static final java.io.File CREDENTIALS_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/sheets-api-app");
    private static FileDataStoreFactory dataStoreFactory;
    private static final JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
    private static HttpTransport transport;
    private static Sheets sheetService;
    private static final List<String> API_SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS);

    static {
        try {
            transport = GoogleNetHttpTransport.newTrustedTransport();
            dataStoreFactory = new FileDataStoreFactory(CREDENTIALS_DIR);
        } catch (Throwable t) {
            t.printStackTrace();
            System.exit(1);
        }
    }

    private static Credential authorize() throws IOException {
        InputStream in = MyApplication.class.getResourceAsStream("/my_secret.json");
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(in));

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(transport, jsonFactory, clientSecrets, API_SCOPES)
                .setDataStoreFactory(dataStoreFactory)
                .setAccessType("offline")
                .build();

        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    }

    private static Sheets getService() throws IOException {
        Credential credential = authorize();
        return new Sheets.Builder(transport, jsonFactory, credential).setApplicationName(APP_NAME).build();
    }

    private static void modifyCell(String sheetId, String cellRange, String valueToInsert) throws Exception {
        sheetService = getService();
        ValueRange valueRange = new ValueRange();
        valueRange.setMajorDimension("ROWS").setRange(cellRange);
        valueRange.setValues(Collections.singletonList(Collections.singletonList(valueToInsert)));
        System.out.println("Updating value range: " + valueRange);
        sheetService.spreadsheets().values().update(sheetId, cellRange, valueRange).setValueInputOption("RAW").execute();
    }

    public static void main(String[] args) throws IOException {
        try {
            String targetSheetId = "123456";
            String targetRange = "G7:G7";
            modifyCell(targetSheetId, targetRange, "available");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Can someone help me figure out what’s going wrong here?

A 404 error often occurs when the sheet ID is incorrect or the sheet has been deleted. Make sure that the sheetId you’re passing to the modifyCell function is valid and corresponds to the correct spreadsheet. Double-check it hasn’t been deleted or renamed, and that this ID is visible and accessible in the Google Sheets UI. Also, ensure that the API key or OAuth2 client ID has appropriate permissions to access the spreadsheet with the specified ID. Sometimes re-authenticating the credentials or clearing the datastore can also solve the issue.

I encountered a similar issue, and it turned out that I missed enabling the Google Sheets API in my Google Cloud console. Even if the spreadsheet ID seems right, make sure the API is enabled in your project. Additionally, ensure that the JSON credentials file includes the correct OAuth 2.0 client secrets and that the scopes are registered properly. Also, take a closer look at the path you provide for the credentials file; sometimes a slight typo there can cause issues.

You might want to check if the range you’re trying to update is actually valid and exists in your sheet. Sometimes if “G7:G7” doesn’t exist or is misspelled, the API might return a 404 error. Double-check the sheet name and range in your spreadsheet.