From 0afe049bd711cfac81227794049119353fcaa4c6 Mon Sep 17 00:00:00 2001 From: Martyn Ranyard Date: Tue, 30 Jun 2020 19:55:06 +0200 Subject: [PATCH] Oauth is painful Signed-off-by: Martyn Ranyard --- internal/webserver/webserver.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/internal/webserver/webserver.go b/internal/webserver/webserver.go index 9c519fa..59c009f 100755 --- a/internal/webserver/webserver.go +++ b/internal/webserver/webserver.go @@ -207,14 +207,22 @@ func TwitchAdminHandler(response http.ResponseWriter, request *http.Request) { vars := mux.Vars(request) if vars["code"] != "" { response.Header().Add("Content-type", "text/plain") - resp, err := http.PostForm( - "https://id.twitch.tv/oauth2/token", - url.Values{ - "client_id": {ircBot.AppCredentials.ClientID}, - "client_secret": {ircBot.AppCredentials.Password}, - "code": {vars["code"]}, - "grant_type": {"authorization_code"}, - "redirect_uri": {"https://" + ircBot.Config.ExternalUrl + "/twitchadmin"}}) + form := url.Values{ + "client_id": {ircBot.AppCredentials.ClientID}, + "client_secret": {ircBot.AppCredentials.Password}, + "code": {vars["code"]}, + "grant_type": {"authorization_code"}, + "redirect_uri": {"https://" + ircBot.Config.ExternalUrl + "/twitchadmin"}} + req, err := http.NewRequest("POST", "https://id.twitch.tv/oauth2/token", strings.NewReader(form.Encode())) + if err != nil { + response.WriteHeader(500) + response.Header().Add("Content-type", "text/plain") + fmt.Fprint(response, "ERROR: "+err.Error()) + return + } + req.Header.Add("Client-ID", ircBot.AppCredentials.ClientID) + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + resp, err := http.DefaultClient.Do(req) if err != nil { response.WriteHeader(500) response.Header().Add("Content-type", "text/plain")