2016-11-07 66 views
0

我試圖從我的後端訪問Google表格,使用Spring MVC編寫。使用教程,我得到這樣的:從Spring MVC訪問Google表格

public static Sheets getSheetsService() throws IOException { 
    return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, authorize()) 
      .setApplicationName(APPLICATION_NAME) 
      .build(); 
} 

public static Credential authorize() throws IOException { 
    // Load client secrets. 
    InputStream in = 
      ConcreteSheetsController.class.getResourceAsStream("/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; 
} 

創建的祕密在於它是這裏所描述的方法:https://developers.google.com/sheets/quickstart/java

這是「工作」,但應用程序的輸出爲我提供了一個鏈接。當我點擊它時,我的瀏覽器會要求我選擇Google帳號。如果我這樣做一切正常,但我想有一個更適合的服務器解決方案。我希望我的secret.json文件能夠讓我的應用程序登錄,而無需提供我自己的用戶帳戶。有什麼辦法可以做到這一點?

+0

你可以嘗試看看服務帳戶在使用[的OAuth 2.0服務器到服務器的應用程序(https://developers.google.com/identity/protocols/OAuth2ServiceAccount)。您可以進行API調用,特別是在調用Cloud API訪問基於項目的數據而不是用戶特定的數據時。 – noogui

回答

0

嘗試使用Gdata第三方庫而不是google.v4。這是我用來傳遞憑據的代碼。你將不得不創建一個P12文件google's console

然後,顯然你的工作表/工作簿的大部分讀/寫將不得不被改變以匹配gdata方法。

我希望這可以幫助,如果沒有請解僱任何問題,我可能會有所幫助。

// Application name 
    private static final String APPLICATION_NAME = "spreadsheet-application-name"; 

    // account info and p12 
    private static final String ACCOUNT_P12_ID = "[email protected]"; 
    private static File P12FILE; 

    private static String ssName = "Spreadsheet name"; 
    private static String wsName = "worksheetname"; //this has to be all lower case for some reason 

    // scopes 
    private static final List<String> SCOPES = Arrays.asList(
      "https://docs.google.com/feeds", 
      "https://spreadsheets.google.com/feeds"); 

    // Spreadsheet API URL 
    private static final String SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/private/full"; 

    private static final URL SPREADSHEET_FEED_URL; 

    static { 
     try { 
      SPREADSHEET_FEED_URL = new URL(SPREADSHEET_URL); 
      P12FILE = getP12File(); 
     } catch (MalformedURLException e) { 
      throw new RuntimeException(e); 
     } 
    } 
    // Authorize- 
    private static Credential authorize() throws Exception { 

     HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
     JsonFactory jsonFactory = new JacksonFactory(); 

     GoogleCredential credential = new GoogleCredential.Builder() 
       .setTransport(httpTransport) 
       .setJsonFactory(jsonFactory) 
       .setServiceAccountId(ACCOUNT_P12_ID) 
       .setServiceAccountPrivateKeyFromP12File(P12FILE) 
       .setServiceAccountScopes(SCOPES) 
       .build(); 

//  boolean ret = credential.refreshToken(); 
     // debug dump 
//  Log.debug("refreshToken:" + ret); 

     // debug dump 
//  if (credential != null) { 
//   Log.debug("AccessToken:" + credential.getAccessToken()); 
//  } 


     return credential; 
    } 

    // Get service 
    private static SpreadsheetService getService() throws Exception { 

     SpreadsheetService service = new SpreadsheetService(APPLICATION_NAME); 
     service.setProtocolVersion(SpreadsheetService.Versions.V3); 

     Credential credential = authorize(); 
     service.setOAuth2Credentials(credential); 

     // debug dump 
//  Log.debug("Schema: " + service.getSchema().toString()); 
//  Log.debug("Protocol: " + service.getProtocolVersion().getVersionString()); 
//  Log.debug("Service Version: " + service.getServiceVersion()); 


     return service; 
    } 

    private static File getP12File(){ 
     ClassLoader classLoader = GoogleSheetsWriter.class.getClassLoader(); 
     return new File(classLoader.getResource("DriveAPITest-bf290e0ee314.p12").getFile()); 
    }