簡介
這系列篇章我們想要透過python將爬蟲爬取的資料丟到後台,供給給後續的動作使用,而這邊的後台也是大家常常使用的Google Sheet,但要把資料丟到Sheet說簡單也是不簡單的,也是要經過一番寒徹骨,而這篇我們就先來講講有關Google API Setting的過程並做個紀錄。
Google API 啟用

首先我們當然要到Google API網頁來進行動做,可以看到上圖是你有啟用且正確進入Dashboard的畫面。

接著我們要新增一個API專案,可以看到上圖紅色箭頭處,點下後會跳出『 Select a project 』視窗,請點選『 新增專案 』。


當你過去有使用過Google API創立過專案會跳到如上圖一的畫面,請再次點選『Create Project』就會跳到圖二的畫面,而沒創建過的人會直接跳到上圖二的畫面。請自行輸入你的『 Project Name 』完成後按下『 Create 』。

讓子彈飛一會兒後,就可以看到剛剛新增的專案出現了,接著請回到本篇文章的圖一,也就是DashBoard畫面。


點選『Select Project -> 剛剛創建的專案』,如上圖一,由於我們要使用Google的服務,Sheet也算其中之一,且要使用他們所提供的API與之進行溝通與使用,所以請點選上圖二紅框框處按鈕『 ENABLE APIS AND SERVICES 』。


由於Google提供的API滿山滿谷,你用手動瀏覽可能要一段時間,這篇可以透過搜尋來找到Google Sheet API選項,找到後請點選進行申請憑證,請點選『ENABLE』。
Google API 憑證

再來我們就要到創建憑證了,如上圖紅框框處,請點選『CREATE CREDENTIALS』。

接著我們依序選擇如上圖的選項,1.Google Sheets API,2.Web Server,3.Application data,4.No, I’m not using them,選擇好後請點選『What credentials do I need?』。

到了憑證申請的第二步驟,『Account Name』接著選擇角色,請選擇『Owner擁有者』,『Service Account ID』會依照你的Account name他會幫你自動生成,可以不用管他,而憑證格式請選擇『JSON』,完成按下『Continue』。

接著要回到『CREATE CREDENTIALS』點下後,要另外新增『OAuth client ID』,這是最重要的憑證區塊,在Python程式碼中,會使用到。

上圖的『Application type』請選擇『Desktop app』,而Name請輸入自己想輸入的名稱,最後按下『Create』。


創建好後,Google會跳出他們產生的ID與Secret,這會在Python程式碼中會使用到,如上圖一。而日後假如忘記你的上述的ID跟Secret可以到Dashboard中的『 OAuth 2.0 Client IDs 』,可以查找細部內容,如紅框框處,點擊找尋。
小結
創建Google API與憑證,看似有點繁瑣,但是必須每一步都要仔細地記下來,才不會在寫程式的時候還要再回頭去處理憑證的問題。而本篇最重要的資訊就是申請憑證完成後所產出的『ID , Secret』這倆參數,而在下一篇會應用上,所以大家一定要拿到這兩個值。