-
Notifications
You must be signed in to change notification settings - Fork 5
/
access.go
94 lines (75 loc) · 2.08 KB
/
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
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
package main
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"net/http"
)
type accessToken struct {
Value string `json:"value"`
Signature string `json:"signature"`
}
type gqlQuery struct {
Query string `json:"query"`
Variables map[string]any `json:"variables"`
}
type gqlResponse struct {
Data json.RawMessage `json:"data"`
Errors []struct {
Message string `json:"message"`
} `json:"errors"`
}
type gqlAccessToken struct {
StreamPlaybackAccessToken struct {
accessToken
} `json:"streamPlaybackAccessToken"`
}
//go:embed access_token.gql
var accessTokenQuery string
func getAcessToken(c *http.Client, channelName string, oAuthToken *string, deviceID *string, variables map[string]any) (*accessToken, error) {
variables["channelName"] = channelName
q := &gqlQuery{
Query: accessTokenQuery,
Variables: variables,
}
qs, err := json.Marshal(q)
if err != nil {
return nil, fmt.Errorf("error marshalling GQL query string: %w", err)
}
req, err := http.NewRequest("POST", gqlURL, bytes.NewReader(qs))
if err != nil {
return nil, err
}
req.Header.Set("Client-ID", clientID)
if oAuthToken != nil {
req.Header.Set("Authorization", "OAuth "+*oAuthToken)
}
if deviceID != nil {
req.Header.Set("Device-ID", *deviceID)
}
res, err := c.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("got non-200 http status code %s while fetching access token", res.Status)
}
var gqlRes gqlResponse
if err = json.NewDecoder(res.Body).Decode(&gqlRes); err != nil {
return nil, fmt.Errorf("error decoding GQL response: %w", err)
}
if len(gqlRes.Errors) != 0 {
var gqlErr string
for i := range gqlRes.Errors {
gqlErr += gqlRes.Errors[i].Message
}
return nil, fmt.Errorf("GQL returned error(s) while fetching access token: %s", gqlErr)
}
var accessToken gqlAccessToken
if err = json.Unmarshal(gqlRes.Data, &accessToken); err != nil {
return nil, fmt.Errorf("error decoding access token: %w", err)
}
return &accessToken.StreamPlaybackAccessToken.accessToken, nil
}