Skip to content

Commit

Permalink
Merge pull request #309 from codephobia/twitch-api-update
Browse files Browse the repository at this point in the history
updating twitch api endpoints
  • Loading branch information
bentranter authored Jan 31, 2020
2 parents bae129b + b8a528c commit 8a9b881
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
39 changes: 22 additions & 17 deletions providers/twitch/twitch.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
)

const (
authURL string = "https://api.twitch.tv/kraken/oauth2/authorize"
tokenURL string = "https://api.twitch.tv/kraken/oauth2/token"
userEndpoint string = "https://api.twitch.tv/kraken/user"
authURL string = "https://id.twitch.tv/oauth2/authorize"
tokenURL string = "https://id.twitch.tv/oauth2/token"
userEndpoint string = "https://api.twitch.tv/helix/users"
)

const (
Expand Down Expand Up @@ -146,8 +146,7 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {
if err != nil {
return user, err
}
req.Header.Set("Accept", "application/vnd.twitchtv.v5+json")
req.Header.Set("Authorization", "OAuth "+s.AccessToken)
req.Header.Set("Authorization", "Bearer "+s.AccessToken)
resp, err := p.Client().Do(req)
if err != nil {
return user, err
Expand All @@ -164,26 +163,32 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {

func userFromReader(r io.Reader, user *goth.User) error {
u := struct {
Name string `json:"name"`
Email string `json:"email"`
Nickname string `json:"display_name"`
AvatarURL string `json:"logo"`
Description string `json:"bio"`
ID string `json:"_id"`
Data []struct {
ID string `json:"id"`
Login string `json:"login"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
ProfileImageURL string `json:"profile_image_url"`
Email string `json:"email"`
} `json:"data"`
}{}

err := json.NewDecoder(r).Decode(&u)
if err != nil {
return err
}

user.Name = u.Name
user.Email = u.Email
user.NickName = u.Nickname
if len(u.Data) != 1 {
return fmt.Errorf("user not found in response")
}

user.Name = u.Data[0].Login
user.Email = u.Data[0].Email
user.NickName = u.Data[0].DisplayName
user.Location = "No location is provided by the Twitch API"
user.AvatarURL = u.AvatarURL
user.Description = u.Description
user.UserID = u.ID
user.AvatarURL = u.Data[0].ProfileImageURL
user.Description = u.Data[0].Description
user.UserID = u.Data[0].ID

return nil
}
Expand Down
6 changes: 3 additions & 3 deletions providers/twitch/twitch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ func Test_BeginAuth(t *testing.T) {
session, err := p.BeginAuth("test_state")
s := session.(*Session)
a.NoError(err)
a.Contains(s.AuthURL, "api.twitch.tv/kraken/oauth2/authorize")
a.Contains(s.AuthURL, "id.twitch.tv/oauth2/authorize")
}

func Test_SessionFromJSON(t *testing.T) {
t.Parallel()
a := assert.New(t)

p := provider()
session, err := p.UnmarshalSession(`{"AuthURL":"https://api.twitch.tv/kraken/oauth2/authorize", "AccessToken":"1234567890"}`)
session, err := p.UnmarshalSession(`{"AuthURL":"https://id.twitch.tv/oauth2/authorize", "AccessToken":"1234567890"}`)
a.NoError(err)

s := session.(*Session)
a.Equal(s.AuthURL, "https://api.twitch.tv/kraken/oauth2/authorize")
a.Equal(s.AuthURL, "https://id.twitch.tv/oauth2/authorize")
a.Equal(s.AccessToken, "1234567890")
}

0 comments on commit 8a9b881

Please sign in to comment.