I'm using golang to add customer in the squareup.com but it always says the probelm of the authorization Can anyone tell me that what is problem in my code:-
func Token(c *gin.Context) {
code := c.Query("code")
splitCode := strings.Split(code, "-")
scope := c.DefaultQuery("scope", "CUSTOMERS_WRITE")
customer := models.Customer{
GivenName: "Amelia",
FamilyName: "Earhart",
CompanyName: "fbgusbd",
Nickname: "kdfbkgjkdf",
EmailAddress: "Amelia.Earhart@example.com",
Address: models.Addresss{
AddressLine1: "500 Electric Ave",
AddressLine2: "Suite 600",
Locality: "New York",
AdministrativeDistrictLevel1: "NY",
PostalCode: "10003",
Country: "US",
},
PhoneNumber: "1-212-555-4240",
ReferenceId: "12",
Note: "a customer",
}
fmt.Println(customer)
bindData, err := json.Marshal(customer)
if err != nil {
panic(err)
}
var jsonStr = []byte(string(bindData))
url := config.APIBaseLive + "v2/customers?code=" + splitCode[1] + "&scope=" + scope
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Add("Authorization", "Bearer "+splitCode[1])
req.Header.Add("Accept", "application/json")
fmt.Println("Request", req)
client := &http.Client{}
resp, err := client.Do(req)
// fmt.Println(resp, err)
if err != nil {
panic(err)
}
defer resp.Body.Close()
resp.Header.Add("Authorization", "Bearer "+splitCode[1])
resp.Header.Add("Accept", "application/json")
fmt.Println("response Status:", resp.Status)
fmt.Println("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println("response Body:", string(body))
}
type Customer struct {
GivenName string `json:"given_name" bson:"given_name"`
FamilyName string `json:"family_name" bson:"family_name"`
CompanyName string `json:"company_name" bson:"company_name"`
Nickname string `json:"nickname" bson:"nickname"`
EmailAddress string `json:"email_address" bson:"email_address"`
Address Addresss `json:"address" bson:"address"`
PhoneNumber string `json:"phone_number" bson:"phone_number"`
ReferenceId string `json:"reference_id" bson:"reference_id"`
Note string `json:"note" bson:"note"`
}
Errors:-
{
"errors": [
{
"category": "AUTHENTICATION_ERROR",
"code": "UNAUTHORIZED",
"detail": "This request could not be authorized."
}
]
}
While hitting https://connect.squareup.com/oauth2/authorize?client_id=your_client_id
then it will direct you to a url redirect url you added with a query string code having a key authenticate key. You will add the redirect url in the dashboard and then by using this code you will called a authenticate user. Can anyone help me too solve this problem?
Thanks.
Check how oauth2 is implemented by squareup https://docs.connect.squareup.com/authz/oauth/how-it-works
Your GO code is BACKEND on that diagram.
https://connect.squareup.com/oauth2/authorize is expected to be called from CLIENT (Browser, Native or Mobile App) that will cause some HTTP redirects in browser to ask user for credentials and other challenges. These credentials are expected to be provided by human using Browser or Web View (chromeless version of browser).
You are likely getting redirect response when you send your request from GO. That is redirect to HTML page where user can enter credentials.
Do not try to implement CLIENT in GO, that should not be possible as whole oauth2
flow assumes user input and should detect and reject backend code.