forked from Hakkin/twitchpipe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
access.go
55 lines (43 loc) · 995 Bytes
/
access.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
package main
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
)
type accessToken struct {
Token string `json:"token"`
Sig string `json:"sig"`
}
func getAcessToken(c *http.Client, username string) (*accessToken, error) {
aURL, err := url.Parse(fmt.Sprintf(accessURL, username))
if err != nil {
return nil, err
}
query := aURL.Query()
query.Set("player_type", "embed")
aURL.RawQuery = query.Encode()
req, err := http.NewRequest("GET", aURL.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Client-ID", clientID)
res, err := c.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
if res.StatusCode != http.StatusNotFound {
return nil, fmt.Errorf("access got http status %s", res.Status)
}
return nil, errors.New("stream does not exist")
}
var token accessToken
err = json.NewDecoder(res.Body).Decode(&token)
if err != nil {
return nil, err
}
return &token, nil
}