Skip to content

Instantly share code, notes, and snippets.

@howtomakeaturn
Last active October 19, 2016 04:57
Show Gist options
  • Save howtomakeaturn/666afc39bfc741466208 to your computer and use it in GitHub Desktop.
Save howtomakeaturn/666afc39bfc741466208 to your computer and use it in GitHub Desktop.
Google Drive SDK notes

Google Drive SDK筆記

儲存觀念

上傳檔案時,一律會是原始檔儲存。 若是用Google文件app開啟檔案,會新增一個轉成Google文件格式的檔案。

(所以會看到多出一個檔案,別嚇到了。)

(仔細看,兩個檔案乍看一樣,其實前面的icon不同。)

檔案格式釐清

Google文件格式的檔案,它的格式就是Google格式,不是.doc檔、不是.txt檔、也不是.pdf檔。

下載檔案

有兩個欄位看似可以用:downloadUrl與webContentLink,但其實不行。這兩個欄位只有原始檔案擁有。 Google文件格式的檔案,需要用exportLinks去指定你所需要的格式,目前支援六種格式:

  • HTML text/html
  • Plain text text/plain
  • Rich text application/rtf
  • Open Office doc application/vnd.oasis.opendocument.text
  • PDF application/pdf
  • MS Word document application/vnd.openxmlformats-officedocument.wordprocessingml.document

想想其實沒錯,不指定格式的話,downloadUrl或webContentLink怎麼知道要給你下載什麼?Google文件原始格式嗎?那你下載了也打不開。 這部份有點confusing啦,Google API的response應該噴exceptions提醒developer那兩個欄位對Google文件檔案沒意義才對。

下載檔案的權限

「能檢視就能下載」。

不論是原始檔、或是Google文件檔的下載連結,點下去之後會根據檔案權限來要求你是否要登入。

下載檔案的連結期效

聽說產生的載點有使用期限?最好每次下載前發送API重新產生載點?

Google文件

來,看看這個Google文件的各格式載點連結:

'exportLinks' => array ( 
    'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'https://docs.google.com/feeds/download/documents/export/Export?id=13XJVQZRm98wjqQ72uQA70LGdMx260Uvj-QjVjsQI5RY&exportFormat=docx', 
    'application/vnd.oasis.opendocument.text' => 'https://docs.google.com/feeds/download/documents/export/Export?id=13XJVQZRm98wjqQ72uQA70LGdMx260Uvj-QjVjsQI5RY&exportFormat=odt', 
    'application/rtf' => 'https://docs.google.com/feeds/download/documents/export/Export?id=13XJVQZRm98wjqQ72uQA70LGdMx260Uvj-QjVjsQI5RY&exportFormat=rtf', 
    'text/html' => 'https://docs.google.com/feeds/download/documents/export/Export?id=13XJVQZRm98wjqQ72uQA70LGdMx260Uvj-QjVjsQI5RY&exportFormat=html', 
    'text/plain' => 'https://docs.google.com/feeds/download/documents/export/Export?id=13XJVQZRm98wjqQ72uQA70LGdMx260Uvj-QjVjsQI5RY&exportFormat=txt', '
    application/pdf' => 'https://docs.google.com/feeds/download/documents/export/Export?id=13XJVQZRm98wjqQ72uQA70LGdMx260Uvj-QjVjsQI5RY&exportFormat=pdf', )

我們來推理。

推理:注意看URL,只是由file id 加上export format當作參數,並沒有任何的驗證token之類的參數,所以重新產生幾遍都一樣。結論:Google文件載點沒有使用期限。

一般檔案

首先看看downloadUrl的字串:

'downloadUrl' => 'https://doc-0k-4o-docs.googleusercontent.com/docs/securesc/i5pg34st0a8pq2ka0d2v83ib8cad7a0p/scbi78tv5v8svqp59e4onvcfiji2rpla/1415239200000/13058876669334088843/00872306418681989836/0B0fjVU_N_s6gaktJcHFzMlE0aHc?e=download&gd=true'

再來看看webContentLink的字串:

'webContentLink' => 'https://docs.google.com/uc?id=0B0fjVU_N_s6gaktJcHFzMlE0aHc&export=download'

我們來推理。

推理:注意看URL,沒有出現file id,並且有看似亂數的token,所以重新產生幾遍未必長一樣。原始檔案載點的期限是未知。

根據Google工程師親自回答: http://stackoverflow.com/questions/11730648/how-long-does-the-google-drive-sdk-short-lived-download-url-exist-for

結論:原始檔案載點有期限,而且沒設好是多久。請在每次下載前重新發送API產生載點。

downloadUrl與webContentLink的差別

根據Google工程師親自回答: http://stackoverflow.com/questions/11730648/how-long-does-the-google-drive-sdk-short-lived-download-url-exist-for

downloadURL會跑一次OAuth2.0驗證使用者程序;webContentLink是用cookie直接驗證。

提醒

Google Drive SDK的官方文件過期了,上面的code跟PHP client library內的不同。 所以把官網的code copy去用會出現「找不到類別」的錯誤。

SDK原始碼在這,遇到錯誤去裏面ctrl+f搜尋一下即可。

https://github.com/google/google-api-php-client/blob/master/src/Google/Service/Drive.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment