2017-09-15 16 views
1

사용자가 Google 드라이브에서 문서를 다운로드하도록 선택할 수 있도록 팝업을 표시하는 Java - JSF 웹 응용 프로그램을 개발 중입니다.방금 ​​검색 한 ID에서 Google 문서 가져 오기

는이를 위해 좀 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와 파일의 링크를 비교 한 결과는 완전히 동일합니다.

편집 2 : 만약 내가 driveService.files().list();이면 빈 목록을 반환합니다.

답변

0

Tanaike 내가 GET 대신 수출을 사용했다뿐만 아니라

그래서이되는 일 executeAndDownloadTo 대신 executeMediaAndDownloadTo 말했듯 : 귀하의 답변에 대한

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 피커 및 JS로 만든 드라이브의 문서에서 선택합니다. 나는 저에게 창조 된 파일 하나와 친구에 의해 창조되었지만 나와 공유 된 파일을 모두 시험해 보았습니다. 아무도 일하지 않았습니다. – Aldeguer