gpt4 book ai didi

security - 如何使用后端 golang 通过 squareup 进行身份验证?

转载 作者:IT王子 更新时间:2023-10-29 02:20:03 26 4
gpt4 key购买 nike

在 sqaureup 应用程序 Aplication_name 的 oauth 选项中,有一个重定向 url,它将使用 QueryString 代码重定向给定的 url。当我在浏览器中点击 https://connect.squareup.com/oauth2/authorize?client_id=YOUR_CLIENT_ID 这个 url 时,它会将我重定向到带有附加代码的 oauth 中的给定 url。然后要获取 access_token,您必须向给定的 url https://connect.squareup.com/oauth2/token 发出 POST 请求与 body

{
"client_id": "YOUR_APPLICATION_ID",
"client_secret": "YOUR_APPLICATION_SECRET",
"code": "M-Q7k-N0Emx_3cBqwbVLTQ",
"redirect_uri": "YOUR_REDIRECT_URI"
}

我也这样做,并通过方法 POST 将 json 数据发送到此 url,但它会给我错误:-

{
"message": "Not Authorized",
"type": "service.not_authorized"
}

我为此使用的 Golang 代码是:-

func Token(c *gin.Context) {
code := c.Query("code") // code be something like:-sq0cgp-wLVQt5HOLfug6xiVdmCDCf
splitCode := strings.Split(code, "-")
token := models.PostToken{
ClientID: "YOUR_APPLICATION_ID",
ClientSecret: "YOUR_APPLICATION_SECRET",
Code: splitCode[1],
RedirectUri: c.Request.Host + c.Request.URL.RequestURI(),
}
bindData, err := json.Marshal(token)
if err != nil {
panic(err)
}
var jsonStr = []byte(string(bindData))
url := "https://connect.squareup.com/oauth2/token"
req, err := http.Post(url, "application/json", bytes.NewBuffer(jsonStr))
fmt.Println(req, err)
}

模型结构:-

type PostToken struct {
ClientID string `json:"client_id" bson:"client_id"`
ClientSecret string `json:"client_secret" bson:"client_secret"`
Code string `json:"code" bson:"code"`
RedirectUri string `json:"redirect_uri" bson:"redirect_uri"`
}

最佳答案

你必须做我提到的一些点:-

  • 首先检查您的应用程序 ID。
  • 在第二个参数 ClientSecret 中,您必须使用 Oauth Application Secret key ,您可以从应用程序仪表板 -> Oauth 选项中获得该 key 。
  • Code 中,您没有发送拆分代码发送您在变量名 code 中获取的简单字符串代码值。
  • 第四个参数是可选的,如文档所述here .

然后你会得到你想要的:D。

关于security - 如何使用后端 golang 通过 squareup 进行身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53718902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com