nicklaw5/helix

How can I use current access token with ValidateToken?

lornajane opened this issue · 7 comments

I have an app that runs while I'm streaming, using this library for Twitch integration (thanks!). I'd like to check if the access token is valid before making API calls, since it's quite likely that my token may expire so I can use the refresh token to update it in that case.

How can I get my current access token from the client in order to check if it is valid? Or is there a way to ask the method itself to use the current token rather than a provided one? Is this a feature that would be useful to others?

Hi !

https://dev.twitch.tv/docs/authentication

« App access tokens expire after about 60 days, so you should check that your app access token is valid by submitting a request to the validation endpoint (see Validating Requests). If your token has expired, generate a new one. »

If the token is expired, you need to regen a new one.
You can only refresh valid token to postponed its expiration

As twitch documentation say, you can call https://id.twitch.tv/oauth2/validate to check if you token is valid

It seems to be done in helix.ValidateToken (in authentication.go)

Thanks! (it's a user access token, I should have said that already). I get the access token, I add it to the client with client.SetUserAccessToken(). Should I also store it somewhere else as well so that I can check if the token is valid with client.ValidateToken(userAccessToken), since I have to supply the value again to that? I feel like I'm missing something, the client has the access token, it uses it to send other requests... can it help me by using the current access token and checking its validity?

the function take a token in parameter, you dont have to set it with SetUserAccessToken to test it

isValid, resp, err := helix.ValidateToken("your-user-access-token")

you will have information if its valid ( isValid boolean ) and details about it ( in the resp )

So the required flow is:

  • get an access token
  • set it on the client so it can make requests
  • also store it separately
  • later, use the separate copy of the value as a parameter to check if the token is still valid

I was hoping that the client might be able to use the copy of the access token value that it already had! I understand that I can pass as a parameter, my question is whether that is the only option. I was hoping to find a getter on the access_token, or an option for the client to re-use the value it already has, or allow me to access it.

Hi @lornajane. I stalked your GitHub profile and stumbled across your Twitch stream yesterday 😃

After watching you're vod, I can understand what you're wanting to do. You expect client.GetUserAccessToken() to return the currently set user access token, right?

As you already know, the client.GetUserAccessToken() accepts an authorization code to request an access token from Twitch. In retrospect, naming that method GetUserAccessToken is not great given you expect it to return the current access token.

What I propose we do is:

  • Rename GetUserAccessToken to RequestUserAccessToken
  • Add two new methods on the client called GetUserAccessToken and GetAppAccessToken for returning the currently set user and app access tokens, respectively.

That would then allow you to do the following:

isValid, resp, err := client.ValidateToken(client.GetUserAccessToken())

Thoughts?

I consider myself stalked :) That sounds great! It sounds like a really breaking change though ... I'd be equally happy with either:

  • client.GetCurrentUserAccessToken() as a non-clashing function name
  • client.ValidateToken() can just use the access token it has anyway and tell me if it's still going to work now or not

Just tag me if there's a branch I should test or anything, I'm happy to help if I can!

@lornajane see #57

I've introduced the breaking change, which is fine because I intend to bump this project to a v1.0 release.

I have left the ValidateToken method unchanged as I can see that some people may not want to set the token on the client in order to validate it.

Feel free to test it or offer any suggested changes. Let me know if this resolves your issue.