Skip to content

Instantly share code, notes, and snippets.

@hojongs
Last active March 22, 2022 06:04
Show Gist options
  • Save hojongs/f623ecfe2dcbf3efb2f9e7672cb2c13e to your computer and use it in GitHub Desktop.
Save hojongs/f623ecfe2dcbf3efb2f9e7672cb2c13e to your computer and use it in GitHub Desktop.
Typescript(Javascript): twitch client credentials flow using simple-oauth2
// https://github.com/lelylan/simple-oauth2
// https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#oauth-client-credentials-flow
async function getAccessToken(): Promise<AccessToken | null> {
const client = new ClientCredentials({
client: {
id: "client-id",
secret: "client-secret",
},
auth: {
tokenHost: "https://id.twitch.tv",
tokenPath: "/oauth2/token",
},
options: {
authorizationMethod: "body",
},
});
try {
return await client.getToken({
scope: ["<scope>"],
});
} catch (error: any) {
console.log("Access Token error", error.message);
return null;
}
}
@hojongs
Copy link
Author

hojongs commented Mar 20, 2022

Example output

AccessToken {
  token: {
    access_token: 'ftc4q7wswutgq2i8klewgh2v37asdf',
    expires_in: 5218099,
    scope: [ 'chat:read', 'chat:edit' ],
    token_type: 'bearer',
    expires_at: 2022-05-20T00:16:20.494Z
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment