nicklaw5 / helix

A Twitch Helix API client written in Go.
MIT License
243 stars 88 forks source link

Not able to use the app token with GetStreams endpoint #3

Closed jscoys closed 6 years ago

jscoys commented 6 years ago

Hi,

Maybe I’m not using it correctly, but I’m getting an app token (I checked), then I pass it to my client and then I’m calling the GetStream endpoint the same way that in the documentation. The weird thing is that I’m getting a rate limit of 30requests per second instead of 120.

jscoys commented 6 years ago

Oh and I tested with the same client-id, app token and secret and I got a ratelimit-rate of 120 with postman with the same endpoint:

ratelimit-limit →120 ratelimit-remaining →119

nicklaw5 commented 6 years ago

Do you mind posting the code you're using so that I can replicate your issue. But don't include your tokens, I have my own.

nicklaw5 commented 6 years ago

Never mind, I've just replicated the issue. It appears the Authorization header is not being attached to the request.

nicklaw5 commented 6 years ago

Actually, this appears to work as expected:

client, err := helix.NewClient(&helix.Options{
    ClientID: "your-client-id",
    UserAccessToken: "your-user-access-token",
})
if err != nil {
    log.Fatal(err)
}

resp, _ := client.GetStreams(&helix.StreamsParams{
    First: 1,
})
fmt.Printf("%+v\n", resp)

Output:

&{ResponseCommon:{StatusCode:200 Error: ErrorStatus:0 ErrorMessage: RateLimit:{Limit:120 Remaining:119 Reset:1528279075} StreamsMetadataRateLimit:{Limit:0 Remaining:0} ClipsCreationRateLimit:{Limit:0 Remaining:0}} Data:{Streams:[{ID:28982088016 UserID:87056709 GameID:29595 CommunityIDs:[] Type:live Title:Mineski vs Vici Gaming China Dota2 Supermajor Playoffs Day 3 (Lower bracket) ViewerCount:60554 StartedAt:2018-06-06 08:17:57 +0000 UTC Language:en ThumbnailURL:https://static-cdn.jtvnw.net/previews-ttv/live_user_pgl_dota2-{width}x{height}.jpg}] Pagination:{Cursor:eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MX19}}}

As you can see from the output the rate limit is 120 with 119 requests remaining.

jscoys commented 6 years ago

Hello thx for your answer.

In my use case I just want to use the AppAccessToken with getstreams endpoint, so not the useraccesstoken. Can you try to reproduce it with that context?

jscoys commented 6 years ago

So here's my code...

I'm taking an appAccessToken:

client, err := helix.NewClient(&helix.Options{ ClientID: myClientID, ClientSecret: myClientSecret, }) if err != nil { // handle error }

    resp, err := client.GetAppAccessToken()
    if err != nil {
        // handle error
    }

    //fmt.Printf("%+v\n", resp)

    var myAppToken string = resp.Data.AccessToken

And then I'm trying to get streams for a specific game: client, err := helix.NewClient(&helix.Options{ ClientID: myClientID, ClientSecret: myClientSecret, AppAccessToken: myAppToken, }) if err != nil { // handle error } resp, err := client.GetStreams(&helix.StreamsParams{ First: 100, GameIDs: []string{streamID}, After: afterPagination, }) if err != nil { // handle error }

In the response I've got a ratelimit of 30... if I do the same request through PostMan I'm getting a 120!

nicklaw5 commented 6 years ago

This appears to be the issue: https://github.com/nicklaw5/helix/blob/master/helix.go#L337-L339

We are only attaching the app access token for the Create Entitlement Grants Upload endpoint, as that is the only endpoint that specifies its use. The Twitch docs aren't very helpful. I wasn't aware that it is possible to use app access tokens for other requests.

jscoys commented 6 years ago

Yeah i know the doc doesn’t mention it’s possible or other endpoint, I spent time to search and understand myself ;-) so can you add the possibility to specify the appAccessToken? I guess that if your have app and user access token passed you take user one, and if you have only the app you use this one?

nicklaw5 commented 6 years ago

Sure can. Did you want to put together a PR yourself? Otherwise I can probably get to it over the weekend?

nicklaw5 commented 6 years ago

@jscoys That's been fixed in fc3312c69cffef1fcd67ce87037af8e287024897. I've also cut a new release so be sure to update your dependency manager if you use one.

Thanks for reporting it.

jscoys commented 6 years ago

Hey thx a lot, sorry was planning to do it tonight after work... you are too fast for me ;-)