diff --git a/go.mod b/go.mod index 0500e31..16f3baf 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.16 require ( github.com/sirupsen/logrus v1.8.1 github.com/spf13/viper v1.7.1 + golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 ) diff --git a/go.sum b/go.sum index 4f9c2aa..3bd0b0d 100644 --- a/go.sum +++ b/go.sum @@ -216,9 +216,11 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859 h1:R/3boaszxrf1GEUWTVDzSKVwLmSJpwZ1yqXm8j0v2QI= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 h1:SVwTIAaPC2U/AvvLNZ2a7OVsmBpC8L5BlwK1whH3hm0= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/main.go b/main.go index 2523f2f..062f96b 100644 --- a/main.go +++ b/main.go @@ -2,13 +2,14 @@ package main import ( "github.com/VladyslavLukyanenko/GopherAlert/configs" + "github.com/VladyslavLukyanenko/GopherAlert/twitchAPI" log "github.com/sirupsen/logrus" ) - func main() { configs.ReadConfig() setupLogger() + twitchAPI.GetTwitchChannelStatus("ArQuel") } func setupLogger() { @@ -18,4 +19,4 @@ func setupLogger() { } else { log.SetLevel(lvl) } -} \ No newline at end of file +} diff --git a/twitchAPI/twitchCommands.go b/twitchAPI/twitchCommands.go new file mode 100644 index 0000000..99b252c --- /dev/null +++ b/twitchAPI/twitchCommands.go @@ -0,0 +1,33 @@ +package twitchAPI + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" + "os" +) + +var ENDPOINT_URL = "https://api.twitch.tv/helix/streams/?user_login=" +var TOKEN = GetTwitchToken() +var CLIENT_ID = clientID + +func GetTwitchChannelStatus(twitchLogin string) string { + + req, _ := http.NewRequest(http.MethodGet, ENDPOINT_URL+twitchLogin, nil) + req.Header.Add("Client-ID", CLIENT_ID) + req.Header.Add("Authorization", "Bearer " + TOKEN) + + response, err := http.DefaultClient.Do(req) + if err != nil { + fmt.Print(err.Error()) + os.Exit(1) + } + + responseData, err := ioutil.ReadAll(response.Body) + if err != nil { + log.Fatal(err) + } + fmt.Println(string(responseData)) + return (string(responseData)) +} diff --git a/twitchAPI/twitchCredentials.go b/twitchAPI/twitchCredentials.go new file mode 100644 index 0000000..452036a --- /dev/null +++ b/twitchAPI/twitchCredentials.go @@ -0,0 +1,31 @@ +package twitchAPI + +import ( + "context" + "fmt" + "log" + + "golang.org/x/oauth2/clientcredentials" + "golang.org/x/oauth2/twitch" + +) + +var ( + clientID = "bug2jjuz0dkbmt1ybs1af6kh1jm968" + clientSecret = "f3yeu9mojrxchu6c2pxtw0lce6d7dt" + oauth2Config *clientcredentials.Config +) + +func GetTwitchToken() string { + oauth2Config = &clientcredentials.Config{ + ClientID: clientID, + ClientSecret: clientSecret, + TokenURL: twitch.Endpoint.TokenURL, + } + + token, err := oauth2Config.Token(context.Background()) + if err != nil { + log.Fatal(err) + } + return token.AccessToken +}