Help with Implementing OAuth Authentication for Custom Twitch App

Hi everyone

I’m working on a custom Twitch app and I’m currently struggling with implementing OAuth authentication for user login. I’ve followed the official documentation for setting up OAuth with the Twitch API, but I’m running into an issue where the access token seems to be expired almost immediately after I generate it.

Here’s a simplified version of what I’m doing:

  1. I send the user to the OAuth URL to authenticate.
  2. The user successfully authenticates and I get the access token in the callback.
  3. I attempt to use the access token immediately, but it returns an error: 401 Unauthorized.

Has anyone else run into this or have suggestions on what I might be missing? Are there any specific steps or parameters I should be checking to make sure the token remains valid?

Thanks!

Which oAuth flow are you using?

This sounds like you got an oAuth code back and then tried to use that code as a token instead of doing step 3 to exchange a code for an access and refresh token.

The normal code flow is:

  1. Create a URL and send the User To Twitch
  2. They come back wtih a ?code if they accepted the link
  3. You exchange the (one use) ?code for an access and refresh token
  4. You use the access token until it’s close to or has expired
  5. Use the refresh token to get a new access (and maybe new refresh token)
  6. Go to step 4

Reference documentation for Authorization code grant flow - Getting OAuth Access Tokens | Twitch Developers

I think you are neglecting to do Use the authorization code to get a token