-
Notifications
You must be signed in to change notification settings - Fork 2
/
getUserInfoByOauth.go
51 lines (43 loc) · 1.28 KB
/
getUserInfoByOauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package repos
import (
"errors"
"encoding/json"
"github.com/khades/servbot/httpclient"
"github.com/khades/servbot/models"
)
type requestError struct {
Error string
Status string
Message string
}
type tokenResponse struct {
Token string `json:"access_token"`
}
type nameResponse struct {
Name string `json:"name"`
ID string `json:"_id"`
Logo string `json:"logo"`
}
func GetUserInfoByOauth(oauthKey *string) (*models.HTTPSession, error) {
cacheKey := "OauthKey:"+*oauthKey
binaryData, found := cacheObject.Get(cacheKey)
if found {
result :=binaryData.(models.HTTPSession)
return &result, nil
}
url := "https://api.twitch.tv/kraken/user?oauth_token=" + *oauthKey
nameResp, err := httpclient.TwitchV5(Config.ClientID, "GET", url, nil)
if err != nil {
return nil, err
}
if nameResp.StatusCode == 400 {
return nil, errors.New("Twitch Error, Cant get username")
}
var usernameStruct = new(nameResponse)
nameMarshallError := json.NewDecoder(nameResp.Body).Decode(usernameStruct)
if nameMarshallError != nil {
return nil, errors.New("Twitch Error, Cant marshall username: "+nameMarshallError.Error())
}
result := models.HTTPSession{Username: usernameStruct.Name, UserID: usernameStruct.ID, Key: *oauthKey, AvatarURL: usernameStruct.Logo}
return &result, nil
}