|
| 1 | +--- |
| 2 | +title: "Kick" |
| 3 | +--- |
| 4 | + |
| 5 | +# Kick |
| 6 | + |
| 7 | +OAuth 2.0 provider for Kick. |
| 8 | + |
| 9 | +Also see the [OAuth 2.0 with PKCE](/guides/oauth2-pkce) guide. |
| 10 | + |
| 11 | +## Initialization |
| 12 | + |
| 13 | +```ts |
| 14 | +import * as arctic from "arctic"; |
| 15 | + |
| 16 | +const kick = new arctic.Kick(clientId, clientSecret, redirectURI); |
| 17 | +``` |
| 18 | + |
| 19 | +## Create authorization URL |
| 20 | + |
| 21 | +```ts |
| 22 | +import * as arctic from "arctic"; |
| 23 | + |
| 24 | +const state = arctic.generateState(); |
| 25 | +const codeVerifier = arctic.generateCodeVerifier(); |
| 26 | +const scopes = ["user:read"]; |
| 27 | +const url = kick.createAuthorizationURL(state, codeVerifier, scopes); |
| 28 | +``` |
| 29 | + |
| 30 | +## Validate authorization code |
| 31 | + |
| 32 | +`validateAuthorizationCode()` will either return an [`OAuth2Tokens`](/reference/main/OAuth2Tokens), or throw one of [`OAuth2RequestError`](/reference/main/OAuth2RequestError), [`ArcticFetchError`](/reference/main/ArcticFetchError), [`UnexpectedResponseError`](/reference/main/UnexpectedResponseError), or [`UnexpectedErrorResponseBodyError`](/reference/main/UnexpectedErrorResponseBodyError). Kick returns an access token, the access token expiration, and a refresh token. |
| 33 | + |
| 34 | +```ts |
| 35 | +import * as arctic from "arctic"; |
| 36 | + |
| 37 | +try { |
| 38 | + const tokens = await kick.validateAuthorizationCode(code, codeVerifier); |
| 39 | + const accessToken = tokens.accessToken(); |
| 40 | + const accessTokenExpiresAt = tokens.accessTokenExpiresAt(); |
| 41 | + const refreshToken = tokens.refreshToken(); |
| 42 | +} catch (e) { |
| 43 | + if (e instanceof arctic.OAuth2RequestError) { |
| 44 | + // Invalid authorization code, credentials, or redirect URI |
| 45 | + const code = e.code; |
| 46 | + // ... |
| 47 | + } |
| 48 | + if (e instanceof arctic.ArcticFetchError) { |
| 49 | + // Failed to call `fetch()` |
| 50 | + const cause = e.cause; |
| 51 | + // ... |
| 52 | + } |
| 53 | + // Parse error |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Refresh access tokens |
| 58 | + |
| 59 | +Use `refreshAccessToken()` to get a new access token using a refresh token. Kick returns the same values as during the authorization code validation. This method also returns `OAuth2Tokens` and throws the same errors as `validateAuthorizationCode()` |
| 60 | + |
| 61 | +```ts |
| 62 | +import * as arctic from "arctic"; |
| 63 | + |
| 64 | +try { |
| 65 | + const tokens = await kick.refreshAccessToken(refreshToken); |
| 66 | + const accessToken = tokens.accessToken(); |
| 67 | + const accessTokenExpiresAt = tokens.accessTokenExpiresAt(); |
| 68 | +} catch (e) { |
| 69 | + if (e instanceof arctic.OAuth2RequestError) { |
| 70 | + // Invalid authorization code, credentials, or redirect URI |
| 71 | + } |
| 72 | + if (e instanceof arctic.ArcticFetchError) { |
| 73 | + // Failed to call `fetch()` |
| 74 | + } |
| 75 | + // Parse error |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Get user profile |
| 80 | + |
| 81 | +Add the `user:read` scope when creating the authorization URL. |
| 82 | + |
| 83 | +```ts |
| 84 | +const scopes = ["user:read"]; |
| 85 | +const url = kick.createAuthorizationURL(state, codeVerifier, scopes); |
| 86 | +``` |
| 87 | + |
| 88 | +Then make a request to the Kick REST API with the access token. |
| 89 | + |
| 90 | +```ts |
| 91 | +const tokens = await kick.validateAuthorizationCode(code, codeVerifier); |
| 92 | + |
| 93 | +const response = await fetch("https://api.kick.com/public/v1/users", { |
| 94 | + headers: { |
| 95 | + Authorization: `Bearer ${tokens.accessToken()}` |
| 96 | + } |
| 97 | +}); |
| 98 | +const user = await response.json(); |
| 99 | +``` |
| 100 | + |
| 101 | +## Revoke tokens |
| 102 | + |
| 103 | +Pass a token to `revokeToken()` to revoke all tokens associated with the authorization. This can throw the same errors as `validateAuthorizationCode()`. |
| 104 | + |
| 105 | +```ts |
| 106 | +try { |
| 107 | + await kick.revokeToken(refreshToken); |
| 108 | +} catch (e) { |
| 109 | + if (e instanceof arctic.OAuth2RequestError) { |
| 110 | + // Invalid authorization code, credentials, or redirect URI |
| 111 | + } |
| 112 | + if (e instanceof arctic.ArcticFetchError) { |
| 113 | + // Failed to call `fetch()` |
| 114 | + } |
| 115 | + // Parse error |
| 116 | +} |
| 117 | +``` |
0 commit comments