diff --git a/main.go b/main.go index b3a0d13094d1be48f3213bff3b1ca06e98060e5d..c6ef60b8cc7346ec621d9ab049e2b22ce812dc37 100644 --- a/main.go +++ b/main.go @@ -117,14 +117,21 @@ func main() { clientSecret := query.Get("client_secret") code := query.Get("code") grantType := query.Get("grant_type") + refreshToken := query.Get("refresh_token") + fmt.Println(refreshToken) // redirectUri := query.Get("redirect_uri") tokenUrl := "https://gw.hml.api.enedis.fr/v1/oauth2/token" + data := url.Values{} data.Set("client_id", clientId) data.Set("client_secret", clientSecret) data.Set("code", code) data.Set("grant_type", grantType) + if refreshToken != "" { + data.Set("refresh_token", refreshToken) + data.Set("grant_type", "refresh_token") + } client := &http.Client{} req, _ := http.NewRequest("POST", tokenUrl, strings.NewReader(data.Encode())) @@ -137,23 +144,27 @@ func main() { fmt.Println(err) } else { defer response.Body.Close() - // Set Content-Type in response header - w.Header().Add("Content-Type", "application/json") - - // Decode response Body using the defined type "TokenResponse" - data := TokenResponse{} - decodeError := json.NewDecoder(response.Body).Decode(&data) - if decodeError != nil { - http.Error(w, decodeError.Error(), 500) - return + if response.StatusCode >= 200 && response.StatusCode <= 299 { + // Set Content-Type in response header + w.Header().Add("Content-Type", "application/json") + + // Decode response Body using the defined type "TokenResponse" + data := TokenResponse{} + decodeError := json.NewDecoder(response.Body).Decode(&data) + if decodeError != nil { + http.Error(w, decodeError.Error(), 500) + return + } + + // Response with json data + jsonError := json.NewEncoder(w).Encode(data) + if jsonError != nil { + http.Error(w, jsonError.Error(), 500) + return + } + } else { + http.Error(w, http.StatusText(response.StatusCode), response.StatusCode) } - - // Response with json data - jsonError := json.NewEncoder(w).Encode(data) - if jsonError != nil { - http.Error(w, jsonError.Error(), 500) - return - } } })