我正在尝试使用Spreadsheets.Values.Update方法更新Google工作表上的单元格。我可以访问谷歌表并读回数据,但是当使用Spreadsheets.Values.Update方法时,我收到以下错误:
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"
}
下面是代码。我使用了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();
}
}
}
答案 0 :(得分:2)
答案 1 :(得分:1)
我只是发现未找到&#34;请求的实体&#34;错误是由无效的spreadsheetId引起的。 所以我假设&#34;未找到请求的实体&#34;错误意味着服务器收到了请求但找不到请求的资源或者它不存在
答案 2 :(得分:0)
请确保您使用的是有效的电子表格ID或有效的访问电子表格的范围。您可以从Google文档中引用有效范围。
https://developers.google.com/sheets/api/guides/authorizing
在使用drive.file范围时,我也遇到了类似的问题,但是在更改为电子表格范围后,问题已解决。