Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelMachado-dev authored Aug 11, 2024
1 parent 3f7f43c commit 56b4c9c
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions utils/twitch.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 56b4c9c

Please sign in to comment.