From 56b4c9c6c511ff0de01c5bf935700b8a402b6dd9 Mon Sep 17 00:00:00 2001 From: Miguel Machado Date: Sun, 11 Aug 2024 18:17:27 -0300 Subject: [PATCH] Add files via upload --- utils/twitch.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 utils/twitch.go diff --git a/utils/twitch.go b/utils/twitch.go new file mode 100644 index 0000000..b54b230 --- /dev/null +++ b/utils/twitch.go @@ -0,0 +1,39 @@ +package utils + +import ( + "encoding/json" + "fmt" + "io" + "net/http" + "strings" + + "github.com/MiguelMachado-dev/disc-go-bot/config" +) + +func GetTwitchAccessToken() (string, error) { + TwitchClientID := config.GetEnv().TWITCH_CLIENT_ID + TwitchClientSecret := config.GetEnv().TWITCH_CLIENT_SECRET + + url := "https://id.twitch.tv/oauth2/token" + payload := strings.NewReader(fmt.Sprintf("client_id=%s&client_secret=%s&grant_type=client_credentials", TwitchClientID, TwitchClientSecret)) + req, _ := http.NewRequest("POST", url, payload) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + + res, err := http.DefaultClient.Do(req) + if err != nil { + return "", fmt.Errorf("failed to send request: %v", err) + } + defer res.Body.Close() + + body, _ := io.ReadAll(res.Body) + + var result map[string]interface{} + if err := json.Unmarshal(body, &result); err != nil { + return "", fmt.Errorf("failed to parse response: %v", err) + } + + if accessToken, ok := result["access_token"].(string); ok { + return accessToken, nil + } + return "", fmt.Errorf("access token not found in response: %v", result) +}