2016-06-07 25 views
0

我試圖在driveOperations()實現中使用Spring社交谷歌庫。每當我嘗試下載文件時,都會出現錯誤。我甚至試圖下載沒有成功的公共共享文件。認證正在工作。如何用spring社交谷歌下載文件?

我已經試過以下變化:

Resource resource = google.driveOperations().downloadFile("uniquedocidhere"); 
// another approach, no error, but getDownloadUrl() is null 
    DriveFile driveFile = google.driveOperations().getFile("uniqeidhere"); 
    google.driveOperations().downloadFile(driveFile); // 404 
// finally trying it with a file drive v2 url gives an invalid URI error 

我添加的車程範圍內,所以我知道這是不是一個問題。當我最初使用應用程序進行身份驗證時,系統會提示我輸入驅動器。

有問題的文件是谷歌電子表格,但我希望有他們下載爲Excel文件。有了股票谷歌SDK,這可以通過獲取exportLinks然後從該URL獲取。而彈簧社會谷歌圖書館有

final Map<String, String> links = driveFile.getExportLinks(); 

的導出鏈接無法使用,因爲downloadFile似乎並不在這裏與網址的工作(或者他們同樣爲空)。

有誰知道如何獲得一個文件下載與春季社交谷歌和驅動器?我還沒有找到涵蓋驅動器的示例代碼,只有谷歌加和任務。

目前使用Spring 1.3.3啓動/彈簧4.2.5與Spring社會谷歌1.0.0發佈

回答

0

春天社會谷歌不支持下載谷歌文檔文件,因爲他們在一個有用的格式不是。元數據上沒有屬性來下載文件,就像其他文件類型一樣。

雖然谷歌自己的SDK可以處理下載導出的格式,如Excel,CSV和PDF,但春季社交谷歌只公開getExportedLinks()屬性來查看URL和類型,但沒有提供實際下載它們的方法。

我現在正在研究分叉和添加方法調用Google Drive v2導出端點或獲取訪問令牌的可能性,以便我可以使用股票谷歌sdk來獲取文件。

__

我發現我可以通過調用其中谷歌是在我的社會形態創建爲

@Bean 
    @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) 
    public Google google(final ConnectionRepository repository) { 
     final Connection<Google> connection = repository.findPrimaryConnection(Google.class); 
     if (connection == null) 
      log.debug("Google connection is null"); 
     else 
      log.debug("google connected"); 
     return connection != null ? connection.getApi() : null; 
    } 

然後我就能夠下載該文件與google.getAccessToken()訪問的訪問令牌這個:

public class LinkedTemplate extends AbstractOAuth2ApiBinding { 
    private String accessToken; 

    public LinkedTemplate() { 
    } 

    public LinkedTemplate(String accessToken) { 
     super(accessToken); 
     this.accessToken = accessToken; 
    } 

    class ExcelConverter extends ByteArrayHttpMessageConverter { 
     public ExcelConverter() { 
      MediaType t = new MediaType("application", "vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
      this.setSupportedMediaTypes(Arrays.asList(t)); 
     } 
    } 

    public void downloadLinkedFile(final String url, final String outputPath) throws IOException { 
     //application/vnd.openxmlformats-officedocument.spreadsheetml.sheet 
     RestTemplate template = getRestTemplate(); 
     template.setMessageConverters(Arrays.asList(new ExcelConverter())); 

     HttpHeaders headers = new HttpHeaders(); 
     headers.setAccept(Arrays.asList(new MediaType("application"))); 

     HttpEntity<String> entity = new HttpEntity<String>(headers); 

     ResponseEntity<byte[]> response = template.exchange(
       url, 
       HttpMethod.GET, entity, byte[].class, "1"); 

     if (response.getStatusCode() == HttpStatus.OK) { 
      Files.write(Paths.get(outputPath), response.getBody()); 
     } 
    } 
} 
+0

我很驚訝春季社交Google不需要訪問令牌來使用API​​。我猜是有公共和私人API? – DavidS

+0

在您提到它之後,API中提供了一個google.getAccessToken()。 –

相關問題