@numerous-vr-11590 here is an example:
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
func main() {
// Replace these variables with your actual values
tokenEndpoint := "
https://auth-server.com/oauth2/token"
clientID := "your-client-id"
clientSecret := "your-client-secret"
redirectURI := "
https://your-app.com/callback"
authorizationCode := "authorization-code-from-auth"
// Create the form data (URL-encoded) for the token request
data := url.Values{}
data.Set("grant_type", "authorization_code")
data.Set("code", authorizationCode)
data.Set("redirect_uri", redirectURI)
data.Set("client_id", clientID)
data.Set("client_secret", clientSecret)
// Create the POST request
req, err := http.NewRequest("POST", tokenEndpoint, strings.NewReader(data.Encode()))
if err != nil {
fmt.Println("Error creating request:", err)
return
}
// Set the appropriate headers for form submission
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// Perform the request
client := &http.Client{}
resp, err :=
client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return
}
defer resp.Body.Close()
// Read the response body
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return
}
// Parse and print the response (assuming JSON response)
if resp.StatusCode == http.StatusOK {
var responseMap map[string]interface{}
if err := json.Unmarshal(body, &responseMap); err != nil {
fmt.Println("Error parsing JSON response:", err)
return
}
fmt.Printf("Token Response: %+v\n", responseMap)
} else {
fmt.Printf("Failed to get token, status: %d, response: %s\n", resp.StatusCode, string(body))
}
}