44 lines
1.0 KiB
Go
44 lines
1.0 KiB
Go
package streams
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"net/http"
|
||
"net/url"
|
||
)
|
||
|
||
type GetStreamKeyResponse struct {
|
||
// A list that contains the channel’s stream key.
|
||
Data []struct {
|
||
// The channel’s stream key.
|
||
StreamKey string `json:"stream_key"`
|
||
} `json:"data"`
|
||
}
|
||
|
||
// Gets the channel’s stream key.
|
||
//
|
||
// Requires a user access token that includes the channel:read:stream_key scope.
|
||
//
|
||
// The broadcaster ID must match the user ID in the access token.
|
||
func (s *Streams) GetStreamKey(ctx context.Context, broadcasterID string) (*GetStreamKeyResponse, error) {
|
||
endpoint := s.baseUrl.ResolveReference(&url.URL{Path: "streams/key", RawQuery: url.Values{"broadcaster_id": {broadcasterID}}.Encode()})
|
||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint.String(), nil)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res, err := s.client.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer res.Body.Close()
|
||
|
||
var data GetStreamKeyResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|