2017-09-17 96 views
2

我试图使用Spreadsheets.Values.Update方法更新Google工作表上的单元格。我能够访问谷歌的表和读取数据回,但使用Spreadsheets.Values.Update方法时,我得到了以下错误:Google Sheets API v4 Spreadsheets.Values.Update返回错误404:未找到请求的实体

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

下面是代码。我使用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(); 
     } 
} 
} 

回答

1

我刚才发现“请求的实体未找到”错误是由无效的spreadsheetId引起的。 所以我假设“请求的实体未找到”错误意味着服务器收到请求,但找不到资源请求或它不存在

+0

或者他可能没有权限阅读它。 – amchacon

+0

但是,您如何修复权限? – Kokodoko