2026/03/04

為什麼 Openclaw 要安裝 Gog Skill? (Gmail API 讓 Openclaw 讀取/發送 email)



Gog 是什麼?為什麼 Openclaw 要安裝 Gog Skill

Gog 是一個命令列工具(CLI),主要用來整合 Google 的 OAuth 2.0 授權流程,並且讓使用者能安全地存取 Gmail API。由於 Gmail 屬於敏感資料,Google 不允許單純使用 API Key 來讀取或修改郵件,必須透過 OAuth 來取得使用者的同意

Openclaw 來說,安裝 Gog 的目的就是:

  • 讓 Openclaw 能透過 Gmail API 自動化處理郵件(例如讀取、修改、設定過濾器)
  • 使用 OAuth 2.0 確保安全性,避免帳號密碼暴露
  • 提供一個標準化的授權流程,方便後續整合其他 Google 服務

換句話說,Gog 就是 Openclaw 與 Google 之間的「橋樑」,沒有 Gog,Openclaw 就無法合法、安全地存取 Gmail。

⚠️安全提醒!!!

使用 Gog 與 Openclaw 時,請不要將 Gmail 帳號密碼或 Google OAuth Client ID/Secret 直接交給 Openclaw

正確做法是將這些敏感資訊存入 .env 檔案或環境變數,Openclaw 會自動讀取並使用
這樣可以避免帳號密碼外洩,確保授權流程安全

.env 範例
# Google OAuth 設定
GOOGLE_CLIENT_ID=303131204180-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET=GOCSPX-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
GOOGLE_REDIRECT_URI=http://127.0.0.1:11334/oauth2/callback

# Gmail 帳號(僅作識別,不存密碼)
GMAIL_ACCOUNT=Yourgmail@gmail.com
GMAIL_PASS=YOURPASSWORD


    常見錯誤

    在 Google Cloud Console 設定 Gog 所需的 OAuth Redirect URI

    步驟一:登入並建立專案

    1. 登入 Google Cloud Console
    2. 在右上角選擇或建立一個新的專案(例如:openclaw-gog

    步驟二:啟用相關 API

    1. 在左側選單選擇 APIs & Services → Library
    2. 搜尋並啟用以下 API:
      • Gmail API
      • Google Drive API
      • Google Sheets API
      • Google Docs API
        (依照 Openclaw 的需求,至少 Gmail API 是必須的,其他可選)

    步驟三:設定 OAuth 同意畫面

    1. APIs & Services → OAuth consent screen 中,選擇 ExternalInternal
    2. 填寫應用程式名稱、支援 Email、隱私政策 URL(測試用可填假網址)
    3. Scopes 加入 Gmail API 權限,例如:
      • email
      • https://www.googleapis.com/auth/gmail.modify
      • https://www.googleapis.com/auth/gmail.settings.basic
      • https://www.googleapis.com/auth/gmail.settings.sharing
      • https://www.googleapis.com/auth/userinfo.email
      • openid
    4. Test users 加入你自己的 Gmail 帳號

    步驟四:建立 OAuth Client ID

    1. 前往 APIs & Services → Credentials
    2. 點選 Create Credentials → OAuth Client ID
    3. 選擇 Web application,並設定名稱
    4. Authorized redirect URIs 中新增:
      http://127.0.0.1/oauth2/callback
      http://localhost/oauth2/callback
      
      ⚠️ 不要設定固定 port,因為 Gog 每次授權都會隨機挑選 port(例如 40197、40799)

    步驟五:下載 Client ID 與 Secret

    1. 建立完成後,下載 JSON 憑證檔案
    2. 在 Gog 的設定中使用這個 Client ID 與 Secret

    ✅ 總結

    • 必須先建立專案並啟用 Gmail API(可選 Drive/Sheets/Docs)
    • 設定 OAuth 同意畫面,加入必要的 Gmail 權限
    • 建立 OAuth Client ID,並在 Redirect URI 加上 http://127.0.0.1/oauth2/callbackhttp://localhost/oauth2/callback
    • 不要設定固定 port,因為 Gog 每次都會隨機挑選

    設定 Gog Skill

    Step 1:取得授權 URL

    在終端機執行:

    gog auth add yourname@gmail.com --services gmail --remote --step 1
    

    Gog 會輸出一個 auth_url,例如:

    https://accounts.google.com/o/oauth2/auth?...&redirect_uri=http://127.0.0.1:40197/oauth2/callback&state=zD8p2DHMb373YNq3O6oRW1boVLU3deU1Php7dcVjx4Y
    

    打開瀏覽器登入並同意授權,Google 會跳轉到本機 callback,例如:

    http://127.0.0.1:40197/oauth2/callback?state=zD8p2DHMb373YNq3O6oRW1boVLU3deU1Php7dcVjx4Y&code=4/0AfrIepBymUI5fQXs69-4NgeB1TOAw7L6OcEpHjvSUFX78ng4El4ItWzu8kcsX-3MB4s5Rw&scope=...&authuser=0&prompt=consent
    

    瀏覽器顯示「Unable to connect」是正常的,因為 Gog 沒有提供網頁內容。這一步的目的只是讓你拿到完整 URL


    Step 2:只保留必要參數

    ⚠️ 重點提醒:不要把整個 URL 原封不動貼進去!

    在 Step 2 的 --auth-url 中,只需要保留:

    • http://127.0.0.1:<port>/oauth2/callback
    • state=...
    • code=...

    其他像 scope=...authuser=...prompt=... 都可以刪掉

    正確範例:

    gog auth add yourname@gmail.com --services gmail --remote --step 2 --auth-url "http://127.0.0.1:40197/oauth2/callback?state=zD8p2DHMb373YNq3O6oRW1boVLU3deU1Php7dcVjx4Y&code=4/0AfrIepBymUI5fQXs69-4NgeB1TOAw7L6OcEpHjvSUFX78ng4El4ItWzu8kcsX-3MB4s5Rw"
    

    常見錯誤

    • 貼上整個 URL(含 scope、authuser、prompt) → Gog 報「manual auth state missing」
    • 使用舊的 Step 1 URL → Gog 報「state reused」
    • Redirect URI 沒有在 Google Cloud Console 設定 → Google 報「Access blocked」

    ✅ 最後總結

    • Gog 是 Openclaw 與 Google OAuth 的橋樑,必須安裝才能合法、安全地存取 Gmail
    • Step 1 → 拿 auth_url,登入 Google
    • Step 2 → 用 redirect URL,但只保留 statecode
    • 不要貼上 scopeauthuserprompt 等多餘參數


    No comments:

    Post a Comment