我正在尝试在golang中为GitHub实现oauth-workflow并使用https://github.com/franela/goreq执行http(s)请求。
GitHub中有一个部分返回code
,您必须使用POST
,code
和client_id
向https://github.com/login/oauth/access_token发出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对象。您的struct字段应以大写字母开头,以便JSON编码器能够对其进行编码。
type goodparam struct {
Code string `json:"code"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
参见this in action。