-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization-token.go
69 lines (60 loc) · 1.55 KB
/
authorization-token.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
package twitch
import (
"net/http"
"net/url"
"time"
)
const (
ExchangeOAuthTokenUrl = "https://id.twitch.tv/oauth2/token"
)
// Token details the access and refresh tokens as well as allowed scopes.
type Token struct {
AccessToken string `json:"access_token"`
ExpiresInSecs int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope []string `json:"scope"`
TokenType string `json:"token_type"`
}
func ExchangeAuthToken(clientId, clientSecret, code, redirect string, timeout time.Duration) (*Token, error) {
vals := url.Values{}
vals.Add("client_id", clientId)
vals.Add("client_secret", clientSecret)
vals.Add("grant_type", "authorization_code")
vals.Add("code", code)
vals.Add("redirect_uri", redirect)
res, err := Run(&Request{
Method: http.MethodPost,
Url: ExchangeOAuthTokenUrl,
Params: vals,
Timeout: timeout,
})
if err != nil {
return nil, err
}
var t Token
if err := DecodeResponse(res, &t); err != nil {
return nil, err
}
return &t, nil
}
func RefreshToken(clientId, clientSecret, refreshToken string, timeout time.Duration) (*Token, error) {
vals := url.Values{}
vals.Add("client_id", clientId)
vals.Add("client_secret", clientSecret)
vals.Add("grant_type", "refresh_token")
vals.Add("refresh_token", refreshToken)
res, err := Run(&Request{
Method: http.MethodPost,
Url: ExchangeOAuthTokenUrl,
Params: vals,
Timeout: timeout,
})
if err != nil {
return nil, err
}
var t Token
if err := DecodeResponse(res, &t); err != nil {
return nil, err
}
return &t, nil
}