Skip to content

Instantly share code, notes, and snippets.

@dgehriger
Last active December 2, 2025 13:31
Show Gist options
  • Select an option

  • Save dgehriger/f9fd2fc33b6642fe12da5d85f50f8661 to your computer and use it in GitHub Desktop.

Select an option

Save dgehriger/f9fd2fc33b6642fe12da5d85f50f8661 to your computer and use it in GitHub Desktop.
# Minimal GitHub Copilot token generator
# Usage:
# iex (Invoke-RestMethod -Uri 'https://gist.githubusercontent.com/dgehriger/f9fd2fc33b6642fe12da5d85f50f8661/raw/get-copilot-token.ps1')
# Step 1: Start device flow
$deviceCodeResponse = Invoke-RestMethod -Method Post -Uri "https://github.com/login/device/code" -Headers @{
"Accept" = "application/json"
} -Body @{
"client_id" = "01ab8ac9400c4e429b23"
"scope" = "user:email"
}
$deviceCode = $deviceCodeResponse.device_code
$userCode = $deviceCodeResponse.user_code
$verificationUri = $deviceCodeResponse.verification_uri
$interval = $deviceCodeResponse.interval
Write-Host ""
Write-Host "GitHub Copilot device authentication" -ForegroundColor Cyan
Write-Host "1. Copy this code: $userCode" -ForegroundColor Green
Write-Host "2. You will now open GitHub's device login page." -ForegroundColor Yellow
Write-Host "3. Paste the code and authorize access." -ForegroundColor Yellow
Write-Host ""
# Copy to clipboard if possible
try {
$userCode | Set-Clipboard
Write-Host "Code copied to clipboard." -ForegroundColor Green
} catch {
Write-Host "Could not copy code to clipboard, copy it manually." -ForegroundColor Yellow
}
# Open browser only after showing the code
Read-Host "Press ENTER to open the GitHub device login page in your browser..." | Out-Null
Start-Process $verificationUri
Write-Host ""
Write-Host "Waiting for authorization..." -ForegroundColor Cyan
# Step 2: Poll for access token
$oauthToken = $null
while (-not $oauthToken) {
Start-Sleep -Seconds $interval
try {
$tokenResponse = Invoke-RestMethod -Method Post -Uri "https://github.com/login/oauth/access_token" -Headers @{
"Accept" = "application/json"
} -Body @{
"client_id" = "01ab8ac9400c4e429b23"
"device_code" = $deviceCode
"grant_type" = "urn:ietf:params:oauth:grant-type:device_code"
} -ErrorAction SilentlyContinue
if ($tokenResponse.access_token) {
$oauthToken = $tokenResponse.access_token
}
} catch {
# ignore and keep polling
}
}
Write-Host "Authorization successful. Printing token..." -ForegroundColor Green
# Print only the token so it can be captured/used by other tools
Write-Output $oauthToken
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment