-
Couldn't load subscription status.
- Fork 96
Description
This library already support auto-refreshing user access tokens via #195. Are we open to doing similar things for App access tokens?
I am using the library in a project that's purely backend and only accesses a couple pieces of public information. There's no need for user consent, and certainly an overkill to wire up an entire frontend for it.
App access tokens expire over time just like user ones. However, there is no separate token refresh process needed - a new token is simply requested when the old one expires. Although it is possible to do this manually by inspecting the returned error code and refreshing the token, this logic seems like a good candidate to be included in the library as it will need to be done on virtually every call.
For example:
// error handling omitted for example
func init() {
var client, err = helix.NewClient(&helix.Options{
ClientID: os.GetEnv("TWITCH_CLIENT_ID"),
ClientSecret: os.GetEnv("TWITCH_CLIENT_SECRET"),
})
// (handle error)
token, err := client.RequestAppAccessToken([]string{})
// (handle error)
client.SetAppAccessToken(token.Data.AccessToken)
}
func foo() error {
streams, err := client.GetStreams(&helix.StreamsParams{UserLogins: []string{login}})
if err != nil {
return err
} else if streams.StatusCode == 401 {
token, err := client.RequestAppAccessToken([]string{})
// (handle err and token.Error)
client.SetAppAccessToken(token.Data.AccessToken)
streams, err = client.GetStreams(&helix.StreamsParams{UserLogins: []string{login}})
// (handle err and token.Error)
}
// do stuff with streams...
}