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的問題。 –

相關問題