nicklaw5/helix

OAuth AppAccessToken seemingly not getting automatically added to request headers.

pyorot opened this issue · 7 comments

(Reposting because may be urgent). Hey, this doesn't seem to work as you say. I have this code:

if time.Now().After(authExpiry) {
	authExpiry = time.Now()
	res, err := twitch.GetAppAccessToken()
	exitIfError(err)
	authExpiry = authExpiry.Add(time.Duration(res.Data.ExpiresIn) * time.Second)
	log.Insta <- fmt.Sprintf("< | token generated: %s (for %ds)", res.Data.AccessToken, res.Data.ExpiresIn)
}
res, err := twitch.GetStreams(&getStreamsParams)
if err == nil && res.StatusCode != 200 {         // reinterpret HTTP error as actual error
	err = fmt.Errorf("HTTP %d: %s", res.StatusCode, res.ErrorMessage)
}
if err != nil {
	log.Insta <- fmt.Sprintf("x | < : %s", err)
}

and my program output

< | token generated: pfzn88vavmt9hlgy6c33aonf2imako (for 5444223s)
x | < : HTTP 401: OAuth token is missing

during the migration test window that just ended (is back to normal now). Any ideas?

Originally posted by @pyorot in #42 (comment)

@pyorot hey man, you have to add the token to the twitch obj,it’s all well fetching one but you also need to use either a app access or user token, when declaring the twitch helix obj, or update the options in some way or form

Do you have a worked example? I was just following this one; I define the helix object using client ID and client secret.

I just found out that this library has not been sending the Bearer token. I found out because resp.GetRateLimit() returned 30 instead of 800.

It seems a lot of oauth code has been re-written instead of using the official oauth2 package, so it doesn't quite behave the same as other packages. I wonder what happens when the app token expires?

Got it to work using this:

ops := &helix.Options{
	ClientID:     config.Config.TwitchClientID.Get(),
	ClientSecret: config.Config.TwitchClientSecret.Get(),
}

client, err = helix.NewClient(ops)
token, err := client.GetAppAccessToken()
client.SetAppAccessToken(token.Data.AccessToken)

Yeah, I also got it to work the same way, confirmed during the 2020/5/4 token requirement window. Leaving the issue open until issues #42, #44, and the usage example are updated accordingly.

This is already documented here.

Please open a PR if you would like to add any further documentation.

The authentication_docs.md usage example is missing the SetAppAccessToken() line. I don't fancy forking the repo just to correct that.

I don't fancy forking the repo just to correct that.

Then stop wasting our time, and either write your own library or use another.