我正在尝试在 golang 中为 GitHub 实现 oauth-workflow 并使用 https://github.com/franela/goreq执行 http(s) 请求。
有一个 GitHub 返回 code
的部分,您必须向 https://github.com/login/oauth/access_token 发出 POST
请求使用 code
、client_id
和 client_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/