duanli9569 2018-12-10 13:26 采纳率: 100%
浏览 92

如何使用golang在Squareup付款网关中添加客户?

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.

  • 写回答

1条回答 默认 最新

  • 普通网友 2018-12-10 14:04
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统