2013-01-09 179 views
1

我必須下載所有Google驅動器文件。我試圖按照此 for Getting download url並使用此代碼的PDF鏈接 我得到downloadurl使用從Google Drive下載pdf文件

file.getDownloadUrl() 

,但是當我打電話下載或轉換成流時,拋出錯誤404:未找到。

我在哪裏丟失..一些身份驗證問題或一些東西超出了..... 下載我用這樣的...

InputStream tempstream = Temp.downloadFile(service, file); 

private static InputStream downloadFile(Drive service, File file) { 
if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0) { 
    try { 
     //download url is : "https://doc-0s-9k-docs.googleusercontent.com/docs/securesc/6rpci9p5rgc1ards1udmrkv5oparm1a2/eoas3kt0uj6afg559kiugg3d6fftlere/1357740000000/06400000604443042302/18001816663358798513/0B2tdn0tkhMowTFVyRzYzaGhWeTQ?h=16653014193614665626&e=download&gd=true" 
    HttpResponse resp = 
     service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())) 
      .execute(); 
    return resp.getContent(); 
    } catch (IOException e) { 
    // An error occurred. 
    e.printStackTrace(); 
    return null; 
    } 
} else { 
    // The file doesn't have any content stored on Drive. 
    return null; 
} 

}

downloadurl 請幫助...我覺得有些我不能用正確的身份驗證調用方法....

回答

0

您必須確保:

  1. 您的服務已獲得授權
  2. 您的下載網址是新鮮的(因爲它們到期)

否則你的代碼是正確的。欲瞭解更多信息:

https://developers.google.com/drive/manage-downloads

+0

謝謝!我能夠獲得Inputstream作爲內容,但它不會生成所需的文件..當我寫入一些輸出流..我怎麼可以保證輸入流是不是正確的? – zytham