﻿# 第一次設定 Google 雲端服務與 OAuth 2.0 Client IDs 的注意事項

要透過 Google Cloud Platform (GCP) 的 OAuth 2.0 Client IDs 存取任何 Google 雲端服務，除了要走過 OAuth 2.0 的授權流程外，每個服務都需要事先啟用才能使用。除此之外，你還需要設定 CONSENT SCREEN 才行，這篇文章我打算來解說第一次設定 Google 服務與 OAuth 2.0 Client IDs 的注意事項。

### 整體設定步驟

基本上第一次使用 Google 服務的人，都要走過以下步驟：

1.  進入 [Google Cloud Console](https://console.cloud.google.com/) 建立第一個**專案** (Project)
    
    ![Google Cloud Console](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/111bfafbe0550f213ee3-230634762-1885e960-a725-42a2-9a9a-11cac9dcd02e.png)
    
2.  設定 **OAuth consent screen** 資訊
    
    ![OAuth consent screen](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/7dd777bf29e3f6a4519e-230632212-f56049d3-915e-43ad-b561-b8944aaa08a9.png)
    
3.  建立 Credentials > OAuth 2.0 Client ID
    
    ![Credentials > OAuth 2.0 Client ID](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/31a0445fe37984b727f9-230624662-f433a141-ed62-4552-b440-aeb585b96595.png)
    
4.  啟用 API 服務
    
    ![啟用 API 服務](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/52b5119a9fdbb94813a7-230620580-e0358a91-8f55-4873-8170-5254b6e96e5b.png)
    

### 設定 OAuth consent screen 資訊

在 GCP 每個 Project 只能設定一組 OAuth consent screen 資訊，也只需要設定一次，且一定要先設定才能使用 OAuth 授權流程，以下是設定步驟：

1.  選擇 `External` 類型
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/ed4abdafcd15851f0fe9-230625125-9cbefa38-3821-4acb-9ae3-762e7e209f7e.png)
    
2.  **OAuth consent screen**: 設定 `App Name` 與其他必填設定
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/840535bf5acc66365cc4-230625379-a781af17-3782-4415-9b5d-627827d7362e.png)
    
3.  **Scopes**: 這裡可以不用設定，直接按下 `SAVE AND CONTINUE` 繼續
    
4.  **Test Users**: 此步驟十分重要，你要將你的 Google 帳號加入到**測試使用者清單**中，之後才能進行 OAuth 測試！
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/9b32031ca51f54030714-230631352-56a85762-73cf-4e54-83ef-38d5108a1397.png)
    
    如果沒有正確加入測試使用者清單的話，使用 OAuth 2.0 授權時就會出現以下錯誤：
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/5a4933ed977f0461322d-230635570-0344ce2d-931e-4565-a6d2-0faae753c210.png)
    

初始設定完成！

## 啟用 API 服務

每一個要透過 OAuth 2.0 Client IDs 存取的服務，都需要事先啟用 API 才能跟 OAuth 進行整合，以下是啟用服務的步驟：

1.  先進入 [Google Cloud Console](https://console.cloud.google.com/) 頁面
    
2.  我以 Google Drive API 為例，請在最上方輸入 `Google Drive` 關鍵字搜尋
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/3cb34b6e4d015bc37d42-230620446-219cee13-9caa-414b-a3e0-83704cb226e7.png)
    
3.  點擊 `ENABLE` 即可啟用 API 服務
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/52b5119a9fdbb94813a7-230620580-e0358a91-8f55-4873-8170-5254b6e96e5b.png)
    

如果要停用服務，可以參考以下步驟：

1.  先進入 [Google Cloud Console](https://console.cloud.google.com/) 頁面
    
2.  我以 Google Drive API 為例，請在最上方輸入 `Google Drive` 關鍵字搜尋
    
3.  點擊 `MANAGE` 進入管理頁面
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/c2a4646c621c17abe68d-230622550-26804430-1c19-48a5-93cb-28b5c517b139.png)
    
4.  點擊 `DISABLE API` 即可停用 API 服務
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/ae38e9db22513b5d114c-230622780-8888f798-07d8-40eb-8b77-bd34d0a5063d.png)
    
    如果有任何資源是透過 Google Drive API 建立的，在 Google Drive API 被停用後可能會很快被刪除。
    
    所有使用此專案的 Credentials (憑證) 呼叫 Google Drive API 的程式碼都將失效！
    
    ![image](https://stwillblogassets.blob.core.windows.net/files/images/external/user-images.githubusercontent.com/f00654d401749c931bdc-230622944-64ec7c0d-7e1e-428a-a99c-10bccf0b3e5e.png)
    

### 相關連結

-   [Setting up your OAuth consent screen](https://support.google.com/cloud/answer/10311615?hl=en)
-   [在 Google 進行身份驗證](https://cloud.google.com/docs/authentication?hl=zh-cn)
-   [使用 API 密鑰進行身份驗證](https://cloud.google.com/docs/authentication/api-keys?hl=zh-cn#securing_an_api_key)
