Skip to content
Snippets Groups Projects
Commit 57fc57bd authored by Yoan VALLET's avatar Yoan VALLET
Browse files

Update main.go - handle refresh token

parent 2ddae294
Branches
No related tags found
No related merge requests found
Pipeline #5268 passed
...@@ -117,14 +117,21 @@ func main() { ...@@ -117,14 +117,21 @@ func main() {
clientSecret := query.Get("client_secret") clientSecret := query.Get("client_secret")
code := query.Get("code") code := query.Get("code")
grantType := query.Get("grant_type") grantType := query.Get("grant_type")
refreshToken := query.Get("refresh_token")
fmt.Println(refreshToken)
// redirectUri := query.Get("redirect_uri") // redirectUri := query.Get("redirect_uri")
tokenUrl := "https://gw.hml.api.enedis.fr/v1/oauth2/token" tokenUrl := "https://gw.hml.api.enedis.fr/v1/oauth2/token"
data := url.Values{} data := url.Values{}
data.Set("client_id", clientId) data.Set("client_id", clientId)
data.Set("client_secret", clientSecret) data.Set("client_secret", clientSecret)
data.Set("code", code) data.Set("code", code)
data.Set("grant_type", grantType) data.Set("grant_type", grantType)
if refreshToken != "" {
data.Set("refresh_token", refreshToken)
data.Set("grant_type", "refresh_token")
}
client := &http.Client{} client := &http.Client{}
req, _ := http.NewRequest("POST", tokenUrl, strings.NewReader(data.Encode())) req, _ := http.NewRequest("POST", tokenUrl, strings.NewReader(data.Encode()))
...@@ -137,23 +144,27 @@ func main() { ...@@ -137,23 +144,27 @@ func main() {
fmt.Println(err) fmt.Println(err)
} else { } else {
defer response.Body.Close() defer response.Body.Close()
// Set Content-Type in response header if response.StatusCode >= 200 && response.StatusCode <= 299 {
w.Header().Add("Content-Type", "application/json") // Set Content-Type in response header
w.Header().Add("Content-Type", "application/json")
// Decode response Body using the defined type "TokenResponse"
data := TokenResponse{} // Decode response Body using the defined type "TokenResponse"
decodeError := json.NewDecoder(response.Body).Decode(&data) data := TokenResponse{}
if decodeError != nil { decodeError := json.NewDecoder(response.Body).Decode(&data)
http.Error(w, decodeError.Error(), 500) if decodeError != nil {
return 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
}
} }
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment