{"meta":{"title":"服务器到服务器身份验证","intro":"当服务需要在没有用户凭据的情况下代表组织发起 Copilot 请求时，请使用短期有效的安装访问令牌。 在GitHub Actions中，改用内置GITHUB_TOKEN。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/enterprise-cloud@latest/copilot","title":"GitHub Copilot"},{"href":"/zh/enterprise-cloud@latest/copilot/how-tos","title":"操作方法"},{"href":"/zh/enterprise-cloud@latest/copilot/how-tos/copilot-sdk","title":"Copilot SDK"},{"href":"/zh/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/auth","title":"Authentication"},{"href":"/zh/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/auth/server-to-server-tokens","title":"服务器间令牌"}],"documentType":"article"},"body":"# 服务器到服务器身份验证\n\n当服务需要在没有用户凭据的情况下代表组织发起 Copilot 请求时，请使用短期有效的安装访问令牌。 在GitHub Actions中，改用内置GITHUB_TOKEN。\n\n<!-- markdownlint-disable GHD046 GHD005 -->\n\n<!-- Suppressed: GHD046 (outdated release terminology), GHD005 (hardcoded data variable) -->\n\n## GitHub Actions\n\n对于组织拥有的仓库中的工作流，授予内置令牌发出 Copilot 请求的权限：\n\n```yaml\npermissions:\n  contents: read\n  copilot-requests: write\n\njobs:\n  copilot:\n    runs-on: ubuntu-latest\n    steps:\n      - uses: actions/checkout@v6\n      - run: your-application\n        env:\n          GITHUB_TOKEN: $\n```\n\n必须启用组织的**允许使用向组织计费的 Copilot CLI** 策略。 此方法不需要GitHub应用或存储的身份验证机密。 有关详细信息，请参阅 [在 GitHub Actions 中使用 GITHUB\\_TOKEN 运行 Copilot CLI](/zh/enterprise-cloud@latest/copilot/how-tos/copilot-cli/use-copilot-cli-in-actions)。\n\n## 其他服务和 CI 系统\n\n对于GitHub Actions之外的服务：\n\n1. 创建GitHub应用，其中**Copilot请求**存储库权限设置为 **“读取和写入**”。\n\n2. 将其安装在应计费的组织上。 当前Copilot权限检查需要**所有存储库**访问权限。\n\n3. 具有存储库 ID 和Copilot权限的 [为 GitHub 应用生成安装访问令牌](/zh/enterprise-cloud@latest/apps/creating-github-apps/authenticating-with-a-github-app/generating-an-installation-access-token-for-a-github-app)：\n\n   ```json\n   {\n     \"repository_ids\": [123456789],\n     \"permissions\": {\n       \"copilot_requests\": \"write\"\n     }\n   }\n   ```\n\n4. 将生成的 `ghs_` 令牌以 `COPILOT_GITHUB_TOKEN` 的形式传递给运行时。\n\n必须为该组织启用来自 GitHub App 安装的 Copilot 请求。 安装令牌在一小时后过期。\n\n> \\[!WARNING]\n> 不要通过 SDK 的 `gitHubToken`或 `github_token`等效选项传递安装令牌。 此选项适用于用户令牌。 安装令牌必须使用运行时环境身份验证路径。\n\n## 配置运行时\n\n以下示例假定铸造的令牌存储在 `INSTALLATION_TOKEN` 中。 它们仅将其传递给子运行时，并禁用回退到存储的用户凭据。\n\n<div class=\"ghd-codetabs\">\n<div class=\"ghd-codetab\" data-lang=\"typescript\" data-label=\"TypeScript\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">TypeScript</div>\n\n```typescript\nimport { CopilotClient, RuntimeConnection } from \"@github/copilot-sdk\";\n\nconst token = process.env.INSTALLATION_TOKEN;\nif (!token) throw new Error(\"INSTALLATION_TOKEN is required\");\n\nconst client = new CopilotClient({\n    connection: RuntimeConnection.forStdio(),\n    env: {\n        ...process.env,\n        COPILOT_GITHUB_TOKEN: token,\n    },\n    useLoggedInUser: false,\n});\n```\n\n</div>\n\n<div class=\"ghd-codetab\" data-lang=\"python\" data-label=\"Python\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">Python</div>\n\n```python\nimport os\n\nfrom copilot import CopilotClient, RuntimeConnection\n\nclient = CopilotClient(\n    connection=RuntimeConnection.for_stdio(),\n    env={**os.environ, \"COPILOT_GITHUB_TOKEN\": os.environ[\"INSTALLATION_TOKEN\"]},\n    use_logged_in_user=False,\n)\n```\n\n</div>\n\n<div class=\"ghd-codetab\" data-lang=\"go\" data-label=\"Go\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">Go</div>\n\n```golang\npackage main\n\nimport (\n    \"log\"\n    \"os\"\n\n    copilot \"github.com/github/copilot-sdk/go\"\n)\n\nfunc main() {\n    token, ok := os.LookupEnv(\"INSTALLATION_TOKEN\")\n    if !ok {\n        log.Fatal(\"INSTALLATION_TOKEN is required\")\n    }\n    client := copilot.NewClient(&copilot.ClientOptions{\n        Connection:      copilot.StdioConnection{},\n        Env:             append(os.Environ(), \"COPILOT_GITHUB_TOKEN=\"+token),\n        UseLoggedInUser: copilot.Bool(false),\n    })\n    _ = client\n}\n```\n\n</div>\n\n<div class=\"ghd-codetab\" data-lang=\"rust\" data-label=\"Rust\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">Rust</div>\n\n```rust\nuse github_copilot_sdk::{ClientOptions, Transport};\n\nfn main() {\n    let token = std::env::var(\"INSTALLATION_TOKEN\").expect(\"INSTALLATION_TOKEN is required\");\n    let options = ClientOptions::new()\n        .with_transport(Transport::Stdio)\n        .with_env([(\"COPILOT_GITHUB_TOKEN\", token)])\n        .with_use_logged_in_user(false);\n    drop(options);\n}\n```\n\n</div>\n\n<div class=\"ghd-codetab\" data-lang=\"dotnet\" data-label=\".NET\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">.NET</div>\n\n```csharp\nusing System.Collections;\nusing GitHub.Copilot;\n\nvar token = Environment.GetEnvironmentVariable(\"INSTALLATION_TOKEN\")\n    ?? throw new InvalidOperationException(\"INSTALLATION_TOKEN is required\");\nvar environment = Environment.GetEnvironmentVariables()\n    .Cast<DictionaryEntry>()\n    .ToDictionary(entry => (string)entry.Key, entry => entry.Value?.ToString() ?? \"\");\nenvironment[\"COPILOT_GITHUB_TOKEN\"] = token;\n\nawait using var client = new CopilotClient(new CopilotClientOptions\n{\n    Connection = RuntimeConnection.ForStdio(),\n    Environment = environment,\n    UseLoggedInUser = false,\n});\n```\n\n</div>\n\n<div class=\"ghd-codetab\" data-lang=\"java\" data-label=\"Java\"><div class=\"ghd-codetab-fallback-label\" role=\"heading\" aria-level=\"3\">Java</div>\n\n```java\nimport com.github.copilot.CopilotClient;\nimport com.github.copilot.rpc.CopilotClientOptions;\nimport java.util.HashMap;\nimport java.util.Objects;\n\nvar environment = new HashMap<>(System.getenv());\nvar token = Objects.requireNonNull(\n    System.getenv(\"INSTALLATION_TOKEN\"), \"INSTALLATION_TOKEN is required\");\nenvironment.put(\"COPILOT_GITHUB_TOKEN\", token);\n\ntry (var client = new CopilotClient(new CopilotClientOptions()\n        .setEnvironment(environment)\n        .setUseLoggedInUser(false))) {\n    // Use the client.\n}\n```\n\n</div>\n\n</div>\n\n对于进程内 FFI，请在加载运行时之前于主机环境中设置 `COPILOT_GITHUB_TOKEN`；不支持每个客户端的环境选项。 对于现有运行时 URI，请在该运行时进程中设置它。\n\n## 刷新令牌\n\n在当前令牌过期之前，先挖掘新的安装令牌。 对于子进程，请使用新环境重启 SDK 客户端。 对于进程内或现有运行时，请使用新令牌重启主机运行时。\n\n## Billing\n\n使用情况将归属于拥有 GitHub App 安装的账户，并向该账户计费。 使用组织级安装可按组织计费；用户账户级安装会将使用量归属于该用户。\n\n## Troubleshooting\n\n| 症状                                                      | 检查                                                     |\n| ------------------------------------------------------- | ------------------------------------------------------ |\n| `401 Unauthorized`                                      | 确认该组织支持适用于 Copilot 的 GitHub App 安装身份验证。                |\n| `403 Resource not accessible by integration` 或涉及用户信息的错误 | 确认安装令牌位于 `COPILOT_GITHUB_TOKEN` 中，而不是 SDK 的显式令牌选项中。    |\n| `403 Forbidden`来自 Copilot API                           | 确认令牌请求包含 `repository_ids` 和 `copilot_requests: write`。 |\n| `403 Forbidden`，带有所需令牌请求                                | 确认应用安装具有 **“所有仓库”** 访问权限，然后生成一个新令牌。                    |\n| 请求的模型不可用                                                | 确认组织的 Copilot 策略允许使用该模型，并且随附的运行时支持它。                   |\n| 错误的帐户计费                                                 | 确认该安装归属于目标组织。                                          |\n\n## 延伸阅读\n\n* [Authentication](/zh/enterprise-cloud@latest/copilot/how-tos/copilot-sdk/auth/authenticate)：其他身份验证方法和优先级\n* [为 GitHub 应用生成安装访问令牌](/zh/enterprise-cloud@latest/apps/creating-github-apps/authenticating-with-a-github-app/generating-an-installation-access-token-for-a-github-app)：GitHub创建应用令牌"}