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

Update main.go for token tests

parent e6559a7f
Branches
No related tags found
No related merge requests found
Pipeline #5210 failed
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"flag" "flag"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"io/ioutil" "io/ioutil"
"strconv" "strconv"
"strings" "strings"
...@@ -19,7 +20,7 @@ func main() { ...@@ -19,7 +20,7 @@ func main() {
mux := http.NewServeMux() mux := http.NewServeMux()
fmt.Println("Server started") fmt.Println("Server started")
mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("New request") fmt.Println("New auth request")
query := r.URL.Query() query := r.URL.Query()
fmt.Println(query) fmt.Println(query)
// host := strings.Split(query.Get("state"), "-")[1] // host := strings.Split(query.Get("state"), "-")[1]
...@@ -93,7 +94,60 @@ func main() { ...@@ -93,7 +94,60 @@ func main() {
fmt.Println(redir) fmt.Println(redir)
http.Redirect(w, r, redir, 302) http.Redirect(w, r, redir, 302)
} }
})
mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("New token request")
query := r.URL.Query()
fmt.Println(query)
clientId := query.Get("client_id")
clientSecret := query.Get("client_secret")
code := query.Get("code")
grantType := query.Get("grant_type")
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)
client := &http.Client{}
r, _ := http.NewRequest("POST", urlStr, strings.NewReader(data.Encode())) // URL-encoded payload
r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
response, err := client.Do(r)
fmt.Println(response.Status)
if err != nil {
fmt.Println(err)
} else {
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err)
}
// Get the response body as a string
fmt.Printf("Response content: %s\n", string(contents))
// pageCode := string([]byte(pageContent[codeStartIndex:codeEndIndex]))
// pageState := string([]byte(pageContent[stateStartIndex:stateEndIndex]))
// pageUsage := string([]byte(pageContent[usageStartIndex:usageEndIndex]))
// Print out the result
// fmt.Printf("Page code: %s\n", pageCode)
// fmt.Printf("Page state: %s\n", pageState)
// fmt.Printf("Page usage: %s\n", pageUsage)
// state := strings.Split(pageState, "-")[0]
// host := strings.Split(pageState, "-")[1]
// redir := "https://" + host + "/accounts/enedisoauth/redirect?code=" + pageCode + "&state="+ state +"&usage_point_id=" + pageUsage
// fmt.Println(redir)
// http.Redirect(w, r, redir, 302)
}
}) })
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment