
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
- 原文地址:https://skae.top/p/copilot-api/
- 原文作者:薄荷布丁
這篇文章教你怎么自己編寫(xiě)代碼,把你的Github Copilot訂閱的API接出來(lái)用。
大致流程是:
- 發(fā)起 Device Flow 請(qǐng)求,獲取
device_code和user_code(XXXX-XXXX)。 - 在瀏覽器訪問(wèn) https://github.com/login/device,輸入
user_code。 - 輪詢(xún)換取
access_token(ghu_開(kāi)頭的 token)。 - 用
access_token獲取短效Copilot token。 - 用
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字段解析得到。