【轉(zhuǎn)】如何調(diào)用Github Copilot訂閱的Chat Completions API


description: "或許你會(huì)需要免費(fèi)的4o"
date: 2026.03.15 10:34
categories:
- AI
tags: [AI, GitHub Copilot, LLM, OpenAI]
keywords: GitHub Copilot, OpenAI, Chat Completions API, Device Flow, access_token, Copilot token



這篇文章教你怎么自己編寫(xiě)代碼,把你的Github Copilot訂閱的API接出來(lái)用。

大致流程是:

  1. 發(fā)起 Device Flow 請(qǐng)求,獲取device_codeuser_codeXXXX-XXXX)。
  2. 在瀏覽器訪問(wèn) https://github.com/login/device,輸入user_code。
  3. 輪詢(xún)換取access_tokenghu_開(kāi)頭的 token)。
  4. access_token獲取短效Copilot token。
  5. Copilot token調(diào)用 Chat Completions API。

1. 發(fā)起 Device Flow 請(qǐng)求

curl -X POST https://github.com/login/device/code \
  -H "Accept: application/json" \
  -d "client_id=<CLIENT_ID>&scope=read:user"

返回:

{
  "device_code": "...",
  "user_code": "XXXX-XXXX",
  "verification_uri": "https://github.com/login/device",
  "expires_in": 900,
  "interval": 5
}

client_id 可以是自己注冊(cè)的 GitHub OAuth App 的 ID,或者也可以直接用已知的 Copilot 客戶(hù)端 ID(見(jiàn)下)。

應(yīng)用 client_id
VS Code Copilot Iv1.b507a08c87ecfe98
GitHub CLI 178c6fc778ccc68e1d6a

2. 打開(kāi)瀏覽器

訪問(wèn) https://github.com/login/device,輸入上一步得到的user_code,完成登錄。

3. 輪詢(xún)換取 access_token

curl -X POST https://github.com/login/oauth/access_token \
  -H "Accept: application/json" \
  -d "client_id=<CLIENT_ID>&device_code=<DEVICE_CODE>&grant_type=urn:ietf:params:oauth:grant-type:device_code"

返回:

{
  "access_token": "gh_xxx",
  "token_type": "bearer",
  "scope": "read:user"
  //(以及一些其它字段,這里不列出)
}

4. 獲取 Copilot token

curl https://api.github.com/copilot_internal/v2/token \
  -H "Authorization: token ghu_xxx" \
  -H "Editor-Version: vscode/1.85.0" \
  -H "Editor-Plugin-Version: copilot/1.155.0" \
  -H "User-Agent: GitHubCopilotChat/0.12.0"

返回:

{
  "agent_mode_auto_approval": true,
  "annotations_enabled": true,
  "azure_only": false,
  "blackbird_clientside_indexing": false,
  "chat_enabled": true,
  "chat_jetbrains_enabled": true,
  "code_quote_enabled": true,
  "code_review_enabled": true,
  "codesearch": true,
  "copilotignore_enabled": false,
  "endpoints": {
    "api": "https://api.business.githubcopilot.com",
    "origin-tracker": "https://origin-tracker.business.githubcopilot.com",
    "proxy": "https://proxy.business.githubcopilot.com",
    "telemetry": "https://telemetry.business.githubcopilot.com"
  },
  "expires_at": 1773238125,
  "individual": false,
  "limited_user_quotas": null,
  "limited_user_reset_date": null,
  "organization_list": [
    "xxx"
  ],
  "prompt_8k": true,
  "public_suggestions": "disabled",
  "refresh_in": 1500,
  "sku": "copilot_for_business_seat_quota",
  "snippy_load_test_enabled": false,
  "telemetry": "disabled",
  "token": "tid=xx;ol=xx;exp=1773238125;sku=copilot_for_business_seat_quota;proxy-ep=proxy.business.githubcopilot.com;st=dotcom;ssc=1;chat=1;cit=1;malfil=1;editor_preview_features=1;agent_mode=1;agent_mode_auto_approval=1;mcp=1;ccr=1;8kp=1;ip=44.243.00.00;asn=AS16509:xxx",
  "tracking_id": "xxx",
  "vsc_electron_fetcher_v2": false,
  "xcode": true,
  "xcode_chat": false
}

此處只需保存token字段的值即可

另外注意到(雖然沒(méi)什么好注意的):

  • 個(gè)人Pro版,學(xué)生包,企業(yè)版等copilot在sku字段會(huì)不同
  • endpoints字段有API的地址,稍后會(huì)用到,但是其亦可以從proxy-ep解析出來(lái)
  • token在半小時(shí)后過(guò)期

5. 調(diào)用 Chat Completions API

curl https://api.business.githubcopilot.com/chat/completions \
  -H "Authorization: Bearer <copilot_token>" \
  -H "Content-Type: application/json" \
  -H "Copilot-Integration-Id: vscode-chat" \
  -H "Editor-Version: vscode/1.85.0" \
  -H "Editor-Plugin-Version: copilot/1.155.0" \
  -H "User-Agent: GitHubCopilotChat/0.12.0" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "hello"}]
  }'

此處的copilot_token即上一步得到的token字段的值。

注意api地址根據(jù)不同訂閱有兩種:

  • https://api.business.githubcopilot.com/chat/completions(企業(yè)版)
  • https://api.individual.githubcopilot.com/chat/completions(個(gè)人版)

可以通過(guò)上一步返回的endpoints.api字段或者proxy-ep字段解析得到。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • """1.個(gè)性化消息: 將用戶(hù)的姓名存到一個(gè)變量中,并向該用戶(hù)顯示一條消息。顯示的消息應(yīng)非常簡(jiǎn)單,如“Hello ...
    她即我命閱讀 5,376評(píng)論 0 6
  • 1、expected an indented block 冒號(hào)后面是要寫(xiě)上一定的內(nèi)容的(新手容易遺忘這一點(diǎn)); 縮...
    庵下桃花仙閱讀 1,104評(píng)論 1 2
  • 一、工具箱(多種工具共用一個(gè)快捷鍵的可同時(shí)按【Shift】加此快捷鍵選取)矩形、橢圓選框工具 【M】移動(dòng)工具 【V...
    墨雅丫閱讀 1,620評(píng)論 0 0
  • 跟隨樊老師和伙伴們一起學(xué)習(xí)心理知識(shí)提升自已,已經(jīng)有三個(gè)月有余了,這一段時(shí)間因?yàn)樘鞖獾脑蛐菡n,順便整理一下之前學(xué)習(xí)...
    學(xué)習(xí)思考行動(dòng)閱讀 1,025評(píng)論 0 2
  • 一臉憤怒的她躺在了床上,好幾次甩開(kāi)了他抱過(guò)來(lái)的雙手,到最后還堅(jiān)決的翻了個(gè)身,只留給他一個(gè)冷漠的背影。 多次嘗試抱她...
    海邊的藍(lán)兔子閱讀 1,026評(píng)論 1 4

友情鏈接更多精彩內(nèi)容