http - 无法在 golang 中完成 GitHub 的 oauth web 工作流

标签 http github oauth go

我正在尝试在 golang 中为 GitHub 实现 oauth-workflow 并使用 https://github.com/franela/goreq执行 http(s) 请求。

有一个 GitHub 返回 code 的部分,您必须向 https://github.com/login/oauth/access_token 发出 POST 请求使用 codeclient_idclient_secret

package main

import "fmt"
import "github.com/franela/goreq"

type param struct {
  code string
  client_id string
  client_secret string
}

func main() {
  params := param {code: "XX", client_id:"XX", client_secret: "XX"}
  req := goreq.Request{
    Method : "POST",
    Uri : "https://github.com/login/oauth/access_token",
    Body : params,
  }
  req.AddHeader("Content-Type", "application/json")
  req.AddHeader("Accept", "application/json")
  res, _ := req.Do()
  fmt.Println(res.Body.ToString())
}

它总是给出 404{"error":"Not Found"} 消息。 在使用 Python 时,我使用相同的输入数据获得了正确的结果。

最佳答案

您正在生成空的 JSON 对象。您的结构字段应以大写字母开头,以便 JSON 编码器能够对其进行编码。

type goodparam struct {
    Code         string `json:"code"`
    ClientId     string `json:"client_id"`
    ClientSecret string `json:"client_secret"`
}

参见 this in action .

关于http - 无法在 golang 中完成 GitHub 的 oauth web 工作流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28083629/

相关文章:

ios - 解码从 HTTP API 请求接收到的数据

ios - 多个请求完成后Alamofire回调

git - GitHub上的origin和upstream有什么区别?

git - 确保 github PR squash merge 提交评论包含问题 ID

github - 如何在 VSCode 中使用私有(private)存储库?

javascript - OAuth "unsupported_grant_type"不和谐 API

http - IE 缓存行为 : response 304 followed by 200's on same resource?

javascript - Node/Express - 如何实现 DELETE 和 PUT 请求

oauth - AspNet Core 重定向循环中的 Giraffe + GitHub OAuth 流程

c# - C# 中的谷歌分析 API