GitHub Copilot API¶
OAuth2 Device Flow implementation for GitHub Copilot.
Functions¶
InitiateDeviceFlow / initiate_device_flow¶
Initiates the OAuth device authorization flow.
Returns: Device code response containing:
- device_code: Device verification code
- user_code: User-friendly code to display
- verification_uri: URL for user authorization
- expires_in: Code expiration time (seconds)
- interval: Polling interval (seconds)
Example:
PollForToken / poll_for_token¶
Polls for access token after user authorization.
Parameters:
- device_code: Device code from initiation
- on_success: Optional callback on successful authentication
Returns: Access token
Behavior:
- Polls every interval seconds
- Handles authorization_pending automatically
- Increases interval by 5s on slow_down error
- Calls on_success callback when complete
ReadTokenFromDisk / read_token_from_disk¶
Reads cached token from official Copilot client.
Returns: Cached token if available
Cache Locations:
- macOS: ~/Library/Application Support/github-copilot/hosts.json
- Linux: ~/.config/github-copilot/hosts.json
- Windows: %APPDATA%\github-copilot\hosts.json
NewClient / Client¶
Creates HTTP client with X-Initiator header injection.
Constants¶
| Name | Value | Description |
|---|---|---|
ClientID |
Iv1.b507a08c87ecfe98 |
GitHub Copilot client ID |
DeviceCodeURL |
https://github.com/login/device/code |
Device code endpoint |
AccessTokenURL |
https://github.com/login/oauth/access_token |
Token exchange endpoint |
Error Handling¶
Common errors:
authorization_pending: User hasn't authorized yet (retry)slow_down: Polling too fast (increase interval)expired_token: Device code expired (restart flow)access_denied: User denied authorization