星期三, 9月 17, 2025

Gemini CLI Login 方式小記

Gemini CLI Login 方式小記




OS: macOS 14.3

Gemini Cli: 0.4.1


今天來實作 Gemini CLI 使用不同登入方式驗證


Gemini CLI 的驗證方式有 3 種

  • Login with Google

    • Best for

      • Individual developers as well as anyone who has a Gemini Code Assist License.

    • Benefits

      • Free tier: 60 requests/min and 1,000 requests/day

      • Gemini 2.5 Pro with 1M token context window

      • No API key management - just sign in with your Google account

      • Automatic updates to latest models

  • Use Gemini API Key

    • Best for

      • Developers who need specific model control or paid tier access

    • Benefits

      • Free tier: 100 requests/day with Gemini 2.5 Pro

      • Model selection: Choose specific Gemini models

      • Usage-based billing: Upgrade for higher limits when needed

  • Vertex AI

    • Best for

      • Enterprise teams and production workloads

    • Best for

      • Enterprise features: Advanced security and compliance

      • Scalable: Higher rate limits with billing account

      • Integration: Works with existing Google Cloud infrastructure

  • 參考官方網站 https://github.com/google-gemini/gemini-cli 


可以在執行 /auth 看到相關選項



驗證參考文件


根據詢問 Gemini CLI 的結果, 驗證順序為

  • Vertex AI > Gemini API Key > Google login

  • 這個部分也可以理解, Vertex AI 是給 企業使用 優先性肯定比較高


==== 首先來實作 Vertex AI 方式驗證 ====


無組織的專案無法使用 API KEY, 所以無法使用 API KEY 方式執行


故要改用應用程式預設憑證  Application Default Credentials  (ADC)


使用以下指令設定

  • gcloud  auth  application-default  login


這樣的方式要在 ~/.zshrc ( 或是 ~/.bashrc )內設定環境變數


export GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_NAME"

# gemini cli 因為要使用 vertex ai 模式還要多設定 GOOGLE_CLOUD_LOCATION

export GOOGLE_CLOUD_LOCATION="us-central1"


  • location 的部分建議設定 us-central1, 我有遇過設定 asia-east1 沒有 Gemini Pro 2.5 模型的設定

  • 如果想要確認相關 log, 可以將 IAM -- > Audit log 內的 Vertex API 的 Data read 打開, 然後在 Log Explorer 觀察


設定好之後, 啟動 gemini

輸入 /auth

選擇 Vertex AI 方式驗證即可



然後可以到 Billing 觀察相關費用



==== 接下來嘗試 Use Gemini API Key ====


從   AI Studio 取得 API Key


點選 Create API key


選取 要建立 API Key 的 GCP 專案


複製 API Key

沒有即時複製到也不同害怕, 在同一個頁面也有相關訊息可以再查看



準備開始實作

這個時候確認你的環境變數 已經 unset  Vetext AI 的相關變數


準備好剛剛建立的 API KEY


export GEMINI_API_KEY="YOUR_GEMINI_API_KEY"


  • 或是將這段寫入 ~/.zshrc 或是 ~/.bashrc 內


使用 gemini 指令

使用 /auth 選取 Use Gemini API Key


沒有意外就可以開始運作了

同時也可以在 AI Studio API Key 那個頁面觀察到 使用情況


或是在專案的 API & Services 觀察



==== 最後回到 Login with Google ====


最後回到 Login with Google

這個也可以參考之前的文章


大概就是要注意兩件事情

  • 會進行 Google 帳號登入驗證

  • 某些情況下要 export  GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"


一樣使用 gemini 指令啟動

使用 /auth 選取驗證方式 -- > Login with Google



這個時候會跳出瀏覽器 要求登入驗證



授權完畢就可以使用


以上整理 3 種登入方式

感覺離  Gemini CLI 又前進一步


~ enjoy it



References