英文:
Unable to complete oauth web workflow for GitHub in golang
问题
我正在尝试在golang中实现GitHub的oauth工作流,并使用https://github.com/franela/goreq执行http(s)请求。
GitHub返回一个code
,你需要使用code
、client_id
和client_secret
向https://github.com/login/oauth/access_token发起一个POST
请求。
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时,我使用相同的输入数据得到了正确的结果。
英文:
I'm trying to implement oauth-workflow for GitHub in golang and using https://github.com/franela/goreq to perform http(s) requests.
There is a section in which GitHub returns a code
and you have to make a POST
request to https://github.com/login/oauth/access_token with code
, client_id
and 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())
}
It is giving 404
with {"error":"Not Found"}
message always.
While using Python, I'm getting the correct results with the same input data.
答案1
得分: 3
你正在生成空的JSON对象。为了使JSON编码器能够对其进行编码,你的结构字段应以大写字母开头。
type goodparam struct {
Code string `json:"code"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
在这个示例中可以看到。
英文:
You are generating empty JSON objects. Your struct fields should start in capitals for the JSON encoder to be able to encode them.
type goodparam struct {
Code string `json:"code"`
ClientId string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
See this in action.
答案2
得分: 0
你应该仔细检查你的'client_secret'和'client_id'(必须正确,因为你已经获取了代码),如果它们正确,显然GitHub会返回HTTP状态码404,如果它们错误。
英文:
You should double check your 'client_secret' and 'client_id' (must be right because you get the code) if it is correct, apparently Github returns HTTP status code 404 if it is wrong.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论