Copilot SDK 服务端令牌认证配置
介绍在 GitHub Actions 及其他 CI 系统中为 Copilot SDK 配置服务端认证的方式,包括授予内置令牌权限、创建并安装 GitHub App、在运行时传入安装令牌并禁用用户凭据回退,以及令牌刷新与计费归属。
## [GitHub 动作](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/server-to-server-tokens#github-actions>) 对于一个组织拥有的存储库中的工作流程,给予内置的代号许可,以便提出“副驾驶”请求: ```yaml permissions: contents: read copilot-requests: write jobs: copilot: runs-on: ubuntu-latest steps: - uses: actions/checkout@v6 - run: your-application env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} ``` 该组织的 **允许使用向该组织开具的CLI副驾驶** 策略必须启用。此方法不需要 GitHub App 或存储认证密钥。详情请参见 [在 GitHub 中使用副驾驶 CLI 与 GITHUB_TOKEN 的动作](<https://docs.github.com/en/copilot/how-tos/copilot-cli/use-copilot-cli-in-actions>). ## [其他服务和CI系统](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/server-to-server-tokens#other-services-and-ci-systems>) 对于GitHub以外的服务,动作: 1. 创建 GitHub 应用程序 **副驾驶请求** 组织权限设置到 **仅读**. 2. 在组织上安装应归属使用并开具账单的功能. 3. [生成 GitHub App 的安装访问令牌](<https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-an-installation-access-token-for-a-github-app>) 在副驾驶许可下: ```json { "permissions": { "organization_copilot_requests": "read" } } ``` 4. 传出结果 `ghs_` SDK 的符号 `gitHubToken`, `github_token`,或等效的选项。对于已有的运行时间 URI ,设置为 `COPILOT_GITHUB_TOKEN` 在运行时间进程上。 组织必须启用来自 GitHub App 安装的副驾驶请求。 安装符在一个小时后失效 。 说明 不需要签名用户 。 `useLoggedInUser: false` 关闭自动证书发现,包括环境可变符号,例如 `COPILOT_GITHUB_TOKEN`,因此通过 SDK 的显式符号选项将安装符号传递给您。 ## [配置运行时间](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/server-to-server-tokens#configure-the-runtime>) 以下例子假设铸造的代币在 `INSTALLATION_TOKEN`。它们只传递给儿童的运行时间,并禁用存储的用户证书。 TypeScript ```typescript import { CopilotClient, RuntimeConnection } from "@github/copilot-sdk"; const token = process.env.INSTALLATION_TOKEN; if (!token) throw new Error("INSTALLATION_TOKEN is required"); const client = new CopilotClient({ connection: RuntimeConnection.forStdio(), gitHubToken: token, useLoggedInUser: false, }); ``` Python 键 ```python import os from copilot import CopilotClient, RuntimeConnection client = CopilotClient( connection=RuntimeConnection.for_stdio(), github_token=os.environ["INSTALLATION_TOKEN"], use_logged_in_user=False, ) ``` 滚开 ```golang package main import ( "log" "os" copilot "github.com/github/copilot-sdk/go" ) func main() { token, ok := os.LookupEnv("INSTALLATION_TOKEN") if !ok { log.Fatal("INSTALLATION_TOKEN is required") } client := copilot.NewClient(&copilot.ClientOptions{ Connection: copilot.StdioConnection{}, GitHubToken: token, UseLoggedInUser: copilot.Bool(false), }) _ = client } ``` 锈 ```rust use github_copilot_sdk::{ClientOptions, Transport}; fn main() { let token = std::env::var("INSTALLATION_TOKEN").expect("INSTALLATION_TOKEN is required"); let options = ClientOptions::new() .with_transport(Transport::Stdio) .with_github_token(token) .with_use_logged_in_user(false); drop(options); } ``` 净值 ```csharp using GitHub.Copilot; var token = Environment.GetEnvironmentVariable("INSTALLATION_TOKEN") ?? throw new InvalidOperationException("INSTALLATION_TOKEN is required"); await using var client = new CopilotClient(new CopilotClientOptions { Connection = RuntimeConnection.ForStdio(), GitHubToken = token, UseLoggedInUser = false, }); ``` 爪哇语 ```java import com.github.copilot.CopilotClient; import com.github.copilot.rpc.CopilotClientOptions; import java.util.Objects; var token = Objects.requireNonNull( System.getenv("INSTALLATION_TOKEN"), "INSTALLATION_TOKEN is required"); try (var client = new CopilotClient(new CopilotClientOptions() .setGitHubToken(token) .setUseLoggedInUser(false))) { // Use the client. } ``` 对于进程内部的FFI,使用相同的符号选项;主机进程中的环境变量符号在当 `useLoggedInUser` 错误。对于一个已有的运行时间 URI, 设置 `COPILOT_GITHUB_TOKEN` 关于运行时间的过程。 ## [刷新符号](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/server-to-server-tokens#refresh-tokens>) 在当前令牌到期前将新安装令牌挂起。 对于一个子进程, 用新令牌重新启动 SDK 客户端。 对于一个进程或已有的运行时间, 用新令牌重新启动主机运行时间 。 ## [计费](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/server-to-server-tokens#billing>) 用法被归为拥有GitHub App安装的组织并开具账单. ## [解决问题](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/server-to-server-tokens#troubleshooting>) | 症状 | 检查 | | --- | --- | | `401 Unauthorized` | 令牌可能过期或无效。 Mint 一个新的安装令牌 。 | | `403` 声明没有启用使用 GitHub App 安装访问符的 Copilot 请求 | 确认组织支持对Copilot的GitHub App安装认证. | | `No GitHub OAuth token or Copilot HMAC key provided` 或 时 间 `Not authenticated` | 确认安装令牌通过 SDK 的显式令牌选项传递。 使用 `useLoggedInUser: false`,运行时间忽略 `COPILOT_GITHUB_TOKEN` 和其他环境可变符号。 | | `403 Forbidden` 来自副驾驶API | 确认安装有组织 **副驾驶请求** 允许,然后用薄荷来铸造新的代币。 | | 请求的模型无法使用 | 确认组织的副驾驶政策允许模型,捆绑的运行时间支持它. | | 记账错误 | 确认安装属于预定组织. | ## [进一步阅读](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/server-to-server-tokens#further-reading>) - [认证](<https://docs.github.com/en/copilot/how-tos/copilot-sdk/auth/authenticate>):其他认证方法和优先顺序 - [生成 GitHub App 的安装访问令牌](<https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-an-installation-access-token-for-a-github-app>): GitHub 应用符号创建
查看原始来源