2017-09-15 13 views
1

私は、ユーザーがGoogleドライブからドキュメントをダウンロードするためにポップアップを表示するJava-JSF Webアプリケーションを開発しています。取得したばかりのIDからGoogle Docを取得する

はそれを行うために、私はいくつかのJSコードがあります。

<script src="filepicker.js"></script> 
<script> 
    function initPicker() { 
     var picker = new FilePicker({ 
      apiKey: 'MY_API_KEY', 
      clientId: my_client_id, 
      buttonEl: document.getElementById('pick'), 
      onSelect : function(file) { 
         if (file.id) { 
          sendLinkToBean(file.id); 
         } else { 
          alert('Unable to download file.'); 
         } 
        } 
       }); 
    } 
</script> 

<a4j:jsFunction name="sendLinkToBean" action="#{gPPersonFormBean.downloadFile()}"> 
    <a4j:param name="param1" assignTo="#{gPPersonFormBean.fileId}"/> 
</a4j:jsFunction> 

file.idは豆が到着するとG.DriveのAPIのように、私はそれを取得しよう:

public void downloadFile(){ 
    try { 
     Drive driveService = getDriveService(); 
     OutputStream outputStream = new ByteArrayOutputStream(); 
     driveService.files().get(fileId) .executeMediaAndDownloadTo(outputStream); 
    } catch (IOException e) { 
     String mess = "downloadFile(): " 
       + (e.getMessage()!=null?". "+e.getMessage():"") 
       + (e.getCause()!=null?". "+e.getCause():""); 
     logger.error(mess); 
    } 
} 

しかし、私はFileNotFoundExceptionを取得します。

com.google.api.client.http.HttpResponseException: 404 Not Found 
{ 
"error": {"theID" 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "notFound", 
    "message": "File not found: 1tvnGWDCse4TFQwIvqEDTlvv2cebfs0C2JBtjTP5A42I.", 
    "locationType": "parameter", 
    "location": "fileId" 
    } 
    ], 
    "code": 404, 
    "message": "File not found: theID." 
} 
} 

これはなぜ起こっているのでしょうか? ありがとうございます。

EDIT:私はちょうどGoogleとファイルのリンクで与えられたものと受信したIDを比較していますが、まったく同じです。

EDIT 2:私がdriveService.files().list();を実行すると、空のリストが返されます。

答えて

0

Tanaikeだから、それがされて働いていた私は、輸出を使う代わりに取得しなければならなかったと述べたが、またexecuteMediaAndDownloadTo

の代わりにexecuteAndDownloadToとして:あなたの答えのための

String fileId = "## file ID ##"; 
OutputStream outputStream = new ByteArrayOutputStream(); 
driveService.files().export(fileId, "application/pdf") 
    .executeAndDownloadTo(outputStream); 
1

Googleドキュメントをダウンロードする場合は、files.exportを使用してください。あなたは次のスクリプトを反映して試すことができますか?

String fileId = "## file ID ##"; 
OutputStream outputStream = new ByteArrayOutputStream(); 
driveService.files().export(fileId, "application/pdf") 
    .executeMediaAndDownloadTo(outputStream); 
  • このサンプルスクリプトと詳細情報がhereです。

ご質問が誤解された場合は、申し訳ありません。

+0

おかげで、それは、404ファイルを同じエラーを与えました見つからない – Aldeguer

+0

@Aldeguerご迷惑をおかけして申し訳ありません。私は今あなたのEDIT2を見ました。ダウンロードしたいファイルがGoogleドライブにあるかどうか尋ねることはできますか? – Tanaike

+0

はい、Google PickerとJSで作成したドライブのドキュメントから選択しています。私は両方とも、私が作成したファイルと仲間が作ったが、私と共有したファイルの両方を試しました。誰も働いていませんでした – Aldeguer

関連する問題