-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusers.go
executable file
·149 lines (122 loc) · 2.94 KB
/
users.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package twitch
import (
"fmt"
"net/url"
"time"
)
type TokenResponse struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
Scope []string `json:"scope"`
}
type UsersService struct {
client *TwitchClient
}
type User struct {
ID string `json:"_id"`
Name string `json:"name"`
DisplayName string `json:"display_name"`
Email string `json:"email"`
IsEmailVerified bool `json:"email_verified"`
Logo string `json:"logo"`
Bio string `json:"bio"`
IsPartnered bool `json:"partnered"`
IsTwitterConnected bool `json:"twitter_connected"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type Users struct {
Total int `json:"_total"`
Users []User `json:"users"`
}
type Follows struct {
CreatedAt time.Time `json:"created_id"`
Notifications bool `json:"notifications"`
Channel Channel
}
func (u *UsersService) GetAccessToken(code string) (string, error) {
var tokenResponse TokenResponse
err := u.client.request(
"POST",
"oauth2/token",
url.Values{
"grant_type": []string{"authorization_code"},
"clientId": []string{u.client.clientId},
"clientSecret": []string{u.client.clientSecret},
"oauthRedirect": []string{u.client.oauthRedirect},
"code": []string{code},
},
"",
&tokenResponse,
)
if err != nil {
return "", err
}
return tokenResponse.AccessToken, nil
}
// Gets a user object based on the OAuth token provided.
// Required scopes: user_read
func (u *UsersService) GetUser(accessToken string) (User, error) {
var user User
u.client.setAccessToken(accessToken)
err := u.client.request(
"GET",
"user",
nil,
"",
&user,
)
if err != nil {
return User{}, err
}
return user, nil
}
// Gets a specified user object.
// Required scopes: none
func (u *UsersService) GetUserByID(userID string) (User, error) {
var user User
err := u.client.request(
"GET",
fmt.Sprintf("users/%s", userID),
nil,
"",
&user,
)
if err != nil {
return User{}, err
}
return user, nil
}
// Gets the user objects for the specified Twitch login names (up to 100).
// If a specified user’s Twitch-registered email address is not verified, null is returned for that user.
// Required scopes: none
func (u *UsersService) GetUsers(usernames []string) (Users, error) {
var users Users
err := u.client.request(
"GET",
"users",
url.Values{
"login": usernames,
},
"",
&users,
)
if err != nil {
return Users{}, err
}
return users, nil
}
func (u *UsersService) GetFollowedChannelInfo(followerId string, channelId string) (Follows, error) {
var follows Follows
err := u.client.request(
"GET",
fmt.Sprintf("users/%s/follows/channels/%s", followerId, channelId),
nil,
"",
&follows,
)
if err != nil {
return Follows{}, err
}
return follows, nil
}