簡體   English   中英

如何通過 Google Drive API 下載文件?

[英]How to download a file via the Google Drive API?

我正在使用 gem google-api-client 通過 Google Drive API 下載文件。

x = Google::Apis::DriveV2
drive = x::DriveService.new
drive.authorization = auth

files = drive.list_files
files.items.each_with_index do |file, index|
    url_to_index = file.export_links.select { |k, v| v if k == 'text/plain' }
    file_content =  open(url_to_index["text/plain"]).read
end

問題是 file_content 返回的是 Google 登錄屏幕,而不是文本/純格式的文件。 當我的 Rails 應用程序打開 URL 時,它似乎無權訪問文本文件。

讓我的 Rails 應用程序以文本格式獲取文件的正確方法是什么?

下載文件中說明

根據您要執行的下載類型(文件、Google 文檔或內容鏈接),您將使用以下網址之一:

  • 下載文件 — files.get with alt=media文件資源
  • 下載並導出 Google Doc — files.export
  • 將用戶鏈接到文件 — 來自文件資源的 webContentLink

下載文件要求用戶至少具有讀取權限。 此外,您的應用程序必須獲得允許讀取文件內容的范圍的授權。

您可以閱讀文檔以獲取更多信息和示例。

查看日志輸出:您需要通過兩個因素身份驗證才能允許從您的應用程序訪問您的 Google Drive 帳戶。

在日志中,您將看到執行此操作所需的信息:link 和 secret_key。

tail -f log/*.log

我只是意識到用於下載的 mimetype 與文件元不同。 請檢查此網址以獲取可用的 mimetype: https : //developers.google.com/drive/api/v3/integrate-open

例如 mimeType : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM