要透過 Google Cloud Platform (GCP) 的 OAuth 2.0 Client IDs 存取任何 Google 雲端服務,除了要走過 OAuth 2.0 的授權流程外,每個服務都需要事先啟用才能使用。除此之外,你還需要設定 CONSENT SCREEN 才行,這篇文章我打算來解說第一次設定 Google 服務與 OAuth 2.0 Client IDs 的注意事項。
整體設定步驟
基本上第一次使用 Google 服務的人,都要走過以下步驟:
-
進入 Google Cloud Console 建立第一個專案 (Project)

-
設定 OAuth consent screen 資訊

-
建立 Credentials > OAuth 2.0 Client ID

-
啟用 API 服務

設定 OAuth consent screen 資訊
在 GCP 每個 Project 只能設定一組 OAuth consent screen 資訊,也只需要設定一次,且一定要先設定才能使用 OAuth 授權流程,以下是設定步驟:
-
選擇 External
類型

-
OAuth consent screen: 設定 App Name
與其他必填設定

-
Scopes: 這裡可以不用設定,直接按下 SAVE AND CONTINUE
繼續
-
Test Users: 此步驟十分重要,你要將你的 Google 帳號加入到測試使用者清單中,之後才能進行 OAuth 測試!

如果沒有正確加入測試使用者清單的話,使用 OAuth 2.0 授權時就會出現以下錯誤:

初始設定完成!
啟用 API 服務
每一個要透過 OAuth 2.0 Client IDs 存取的服務,都需要事先啟用 API 才能跟 OAuth 進行整合,以下是啟用服務的步驟:
-
先進入 Google Cloud Console 頁面
-
我以 Google Drive API 為例,請在最上方輸入 Google Drive
關鍵字搜尋

-
點擊 ENABLE
即可啟用 API 服務

如果要停用服務,可以參考以下步驟:
-
先進入 Google Cloud Console 頁面
-
我以 Google Drive API 為例,請在最上方輸入 Google Drive
關鍵字搜尋
-
點擊 MANAGE
進入管理頁面

-
點擊 DISABLE API
即可停用 API 服務

如果有任何資源是透過 Google Drive API 建立的,在 Google Drive API 被停用後可能會很快被刪除。
所有使用此專案的 Credentials (憑證) 呼叫 Google Drive API 的程式碼都將失效!

相關連結