2

我有一个用例,我需要获取新创建的Google Spreadsheet的Spreadsheet ID。我参考了Google Docs,发现Google Sheets API v4中的Spreadsheet.create方法会返回Spreadsheet ID。Java ::使用最新Google Sheets v4以编程方式创建电子表格API

但是,我无法得到如何在Java中使用此方法。

此外,参考Google文档(Link),我也知道我还需要OAuth授权。请帮助我了解如何使用Java设置OAuth授权,或者如果存在使用Java和OAuth创建Google电子表格的现有示例,请分享。

回答

0

请遵循表单API文档中的Java Quickstart。授权部分包含在快速入门中。

下面是针对Java凭据部分片段:

public 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; 
    } 
+0

你还没有回答周围的电子表格ID OP的问题。 –

相关问题