Spaces:
Running
Running
Download src/services/github/poll-access-token.ts from imspsycho/copilot-api: direct link, hf CLI and curl.
- Browser
- Download file 1.48 kB
-
https://huggingface.co/spaces/imspsycho/copilot-api/resolve/97a73c4e00f8e59ad1faf3fb74c28d9dbd106911/src/services/github/poll-access-token.ts
- Command line
-
hf download hf://spaces/imspsycho/copilot-api@97a73c4e00f8e59ad1faf3fb74c28d9dbd106911/src/services/github/poll-access-token.ts
-
curl -L -o poll-access-token.ts https://huggingface.co/spaces/imspsycho/copilot-api/resolve/97a73c4e00f8e59ad1faf3fb74c28d9dbd106911/src/services/github/poll-access-token.ts
1.48 kB
| import consola from "consola" | |
| import { | |
| GITHUB_BASE_URL, | |
| GITHUB_CLIENT_ID, | |
| standardHeaders, | |
| } from "~/lib/api-config" | |
| import { sleep } from "~/lib/utils" | |
| import type { DeviceCodeResponse } from "./get-device-code" | |
| export async function pollAccessToken( | |
| deviceCode: DeviceCodeResponse, | |
| ): Promise<string> { | |
| // Interval is in seconds, we need to multiply by 1000 to get milliseconds | |
| // I'm also adding another second, just to be safe | |
| const sleepDuration = (deviceCode.interval + 1) * 1000 | |
| consola.debug(`Polling access token with interval of ${sleepDuration}ms`) | |
| while (true) { | |
| const response = await fetch( | |
| `${GITHUB_BASE_URL}/login/oauth/access_token`, | |
| { | |
| method: "POST", | |
| headers: standardHeaders(), | |
| body: JSON.stringify({ | |
| client_id: GITHUB_CLIENT_ID, | |
| device_code: deviceCode.device_code, | |
| grant_type: "urn:ietf:params:oauth:grant-type:device_code", | |
| }), | |
| }, | |
| ) | |
| if (!response.ok) { | |
| await sleep(sleepDuration) | |
| consola.error("Failed to poll access token:", await response.text()) | |
| continue | |
| } | |
| const json = await response.json() | |
| consola.debug("Polling access token response:", json) | |
| const { access_token } = json as AccessTokenResponse | |
| if (access_token) { | |
| return access_token | |
| } else { | |
| await sleep(sleepDuration) | |
| } | |
| } | |
| } | |
| interface AccessTokenResponse { | |
| access_token: string | |
| token_type: string | |
| scope: string | |
| } | |