{"meta":{"title":"Authentification serveur à serveur","intro":"Utilisez un jeton d'accès d'installation de courte durée lorsqu'un service doit effectuer des demandes Copilot pour le compte d'une organisation sans informations d'identification d'un utilisateur. Dans GitHub Actions, utilisez plutôt le composant GITHUB_TOKEN intégré.","product":"GitHub Copilot","breadcrumbs":[{"href":"/fr/copilot","title":"GitHub Copilot"},{"href":"/fr/copilot/how-tos","title":"Procédures"},{"href":"/fr/copilot/how-tos/copilot-sdk","title":"Kit de développement logiciel (SDK) Copilot"},{"href":"/fr/copilot/how-tos/copilot-sdk/auth","title":"Authentification"},{"href":"/fr/copilot/how-tos/copilot-sdk/auth/server-to-server-tokens","title":"Jetons serveur à serveur"}],"documentType":"article"},"body":"# Authentification serveur à serveur\n\nUtilisez un jeton d'accès d'installation de courte durée lorsqu'un service doit effectuer des demandes Copilot pour le compte d'une organisation sans informations d'identification d'un utilisateur. Dans GitHub Actions, utilisez plutôt le composant GITHUB_TOKEN intégré.\n\n<!-- markdownlint-disable GHD046 GHD005 -->\n\n<!-- Suppressed: GHD046 (outdated release terminology), GHD005 (hardcoded data variable) -->\n\n## GitHub Actions\n\nPour les workflows d’un référentiel appartenant à l’organisation, accordez au jeton intégré l’autorisation d’effectuer des requêtes à 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\nLa stratégie de l’organisation **Autoriser l’utilisation de Copilot CLI facturée à l’organisation** doit être activée. Cette approche n’a pas besoin d’une application GitHub ou d’un secret d’authentification stocké. Pour plus d’informations, consultez [Utilisation de l’interface CLI Copilot dans GitHub Actions avec GITHUB\\_TOKEN](/fr/copilot/how-tos/copilot-cli/use-copilot-cli-in-actions).\n\n## Autres services et systèmes CI\n\nPour les services en dehors de GitHub Actions :\n\n1. Créez une application GitHub avec l’autorisation de dépôt **Requêtes Copilot** définie sur **Lecture et écriture**.\n\n2. Installez-le sur l’organisation qui doit être facturée. La vérification des autorisations Copilot actuelle nécessite **l’accès à tous les référentiels**.\n\n3. [Génération d’un jeton d’accès d’installation pour une application GitHub](/fr/apps/creating-github-apps/authenticating-with-a-github-app/generating-an-installation-access-token-for-a-github-app) avec un ID de référentiel et l’autorisation Copilot :\n\n   ```json\n   {\n     \"repository_ids\": [123456789],\n     \"permissions\": {\n       \"copilot_requests\": \"write\"\n     }\n   }\n   ```\n\n4. Transmettez le jeton résultant `ghs_` au runtime en tant que `COPILOT_GITHUB_TOKEN`.\n\nL’organisation doit être activée pour les requêtes Copilot provenant des installations de GitHub App. Les jetons d’installation expirent après une heure.\n\n> \\[!WARNING]\n> Ne transmettez pas de jeton d’installation via l’option `gitHubToken`, `github_token` ou une option équivalente du SDK. Cette option concerne les jetons utilisateur. Les jetons d’installation doivent utiliser le chemin d’authentification de l’environnement d’exécution.\n\n## Configurer le runtime\n\nLes exemples suivants supposent que le jeton créé se trouve dans `INSTALLATION_TOKEN`. Ils le transmettent uniquement à l’environnement d’exécution enfant et désactivent le recours aux informations d’identification utilisateur stockées.\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\nPour la FFI dans le processus, définissez `COPILOT_GITHUB_TOKEN` dans l’environnement hôte avant de charger l’environnement d’exécution ; les options d’environnement propres à chaque client ne sont pas prises en charge. Pour un URI d’exécution existant, définissez-le sur ce processus d’exécution.\n\n## Jetons d’actualisation\n\nGénérez un nouveau jeton d'installation avant que le jeton actuel n'expire. Pour un processus enfant, redémarrez le client SDK dans le nouvel environnement. Pour un runtime in-process ou existant, redémarrez le runtime hôte avec le nouveau jeton.\n\n## Billing\n\nL’utilisation est attribuée et facturée au compte propriétaire de l’installation de l’application GitHub. Utilisez une installation pour une organisation pour la facturation de l’organisation ; une installation pour un compte utilisateur attribue l’utilisation à cet utilisateur.\n\n## Résolution des problèmes\n\n| Symptôme                                                                                            | Vérifier                                                                                                                                               |\n| --------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |\n| `401 Unauthorized`                                                                                  | Vérifiez que l’organisation prend en charge l’authentification d’installation de l’application GitHub pour Copilot.                                    |\n| `403 Resource not accessible by integration` ou une erreur mentionnant les informations utilisateur | Vérifiez que le jeton d’installation se trouve dans `COPILOT_GITHUB_TOKEN`, et non l’option de jeton explicite du Kit de développement logiciel (SDK). |\n| `403 Forbidden`à partir de l’API Copilot                                                            | Vérifiez que la demande de jeton contient `repository_ids` et `copilot_requests: write`.                                                               |\n| `403 Forbidden` avec la demande de jeton requise                                                    | Vérifiez que l’installation de l’application a accès à **Tous les dépôts**, puis générez un nouveau jeton.                                             |\n| Le modèle demandé n’est pas disponible                                                              | Vérifiez que la stratégie de Copilot de l'organisation autorise le modèle et le runtime groupé le prend en charge.                                     |\n| Compte incorrect facturé                                                                            | Vérifiez que l’installation appartient à l’organisation prévue.                                                                                        |\n\n## Lectures complémentaires\n\n* [Authentification](/fr/copilot/how-tos/copilot-sdk/auth/authenticate) : autres méthodes d’authentification et priorité\n* [Génération d’un jeton d’accès d’installation pour une application GitHub](/fr/apps/creating-github-apps/authenticating-with-a-github-app/generating-an-installation-access-token-for-a-github-app) : création de jetons d’application GitHub"}