无法完成使用Golang进行GitHub的OAuth Web工作流程。

huangapple go评论97阅读模式
英文:

Unable to complete oauth web workflow for GitHub in golang

问题

我正在尝试在golang中实现GitHub的oauth工作流,并使用https://github.com/franela/goreq执行http(s)请求。

GitHub返回一个code,你需要使用codeclient_idclient_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.

huangapple
  • 本文由 发表于 2015年1月22日 15:49:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/28083629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定