登录
首页 >  Golang >  Go问答

使用后端的 Golang 通过 Squareup 进行身份验证的方法

来源:stackoverflow

时间:2024-03-15 15:12:26 202浏览 收藏

怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《使用后端的 Golang 通过 Squareup 进行身份验证的方法》,涉及到,有需要的可以收藏一下

问题内容

在 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 请求

{
  "client_id": "your_application_id",
  "client_secret": "your_application_secret",
  "code": "m-q7k-n0emx_3cbqwbvltq",
  "redirect_uri": "your_redirect_uri"
}

我也这样做,并使用 json 数据通过方法 post 发送到此 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 应用程序密钥,您可以从应用程序仪表板 -> Oauth 选项中获取该密钥。
  • code 中,您无需发送分割代码,而是发送简单的字符串代码值(您在变量名称 code 中获取该值)。
  • 第四个参数是可选的,正如文档中所说的 here

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

以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>