oauth2无法获取令牌:错误的请求

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

oauth2 cannot fetch token: bad request

问题

我编写了一个处理程序,当访问路由/auth/google/callback时,我尝试通过OAuth2使用Google帐户进行登录。处理程序的实现如下:

package route

import (
	"net/http"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
	"fmt"
)

func GoogleOAuthHandler(w http.ResponseWriter, r *http.Request) {
	conf := &oauth2.Config{
		ClientID:     "myclientid",
		ClientSecret: "myclientsecret",
		RedirectURL:  "http://localhost:3000",
		Scopes: []string{
			"https://www.googleapis.com/auth/userinfo.profile",
			"https://www.googleapis.com/auth/userinfo.email",
		},
		Endpoint: google.Endpoint,
	}

	code := r.URL.Query().Get("code")

	token, err := conf.Exchange(oauth2.NoContext, code)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	fmt.Println(token)

	http.Redirect(w, r, "/", http.StatusMovedPermanently)
}

func main()中,设置了http.HandleFunc("/auth/google/callback", route.GoogleOAuthHandler)

当我访问该路径时,浏览器会显示以下错误:

oauth2: cannot fetch token: 400 Bad Request
Response: {
  "error" : "invalid_request",
  "error_description" : "Missing required parameter: code"
}

我是否遗漏了什么?请指导我正确访问OAuth2并从Google帐户获取令牌和信息。

英文:

I write a handler so that when accessing the route /auth/google/callback, I try to login with Google account through OAuth2. The handler is implemented like this:

package route

import (
	"net/http"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
	"fmt"
)

func GoogleOAuthHandler(w http.ResponseWriter, r *http.Request) {
	conf:=&oauth2.Config{
		ClientID:"myclientid",
		ClientSecret:"myclientsecret",
		RedirectURL:"http://localhost:3000",
		Scopes:[]string{
			"https://www.googleapis.com/auth/userinfo.profile",
            "https://www.googleapis.com/auth/userinfo.email",
		},
		Endpoint:google.Endpoint,
	}

	code := r.URL.Query().Get("code")

	token, err := conf.Exchange(oauth2.NoContext, code)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	fmt.Println(token)

	http.Redirect(w, r, "/", http.StatusMovedPermanently)
}

In func main(), http.HandleFunc("/auth/google/callback",route.GoogleOAuthHandler) is setup

When I access that path, it throws an error like this on browser:

oauth2: cannot fetch token: 400 Bad Request
Response: {
  "error" : "invalid_request",
  "error_description" : "Missing required parameter: code"
}

Did I miss something? Please instruct me to make a proper access to OAuth2 and get token and information from Google account

答案1

得分: 1

您正在尝试访问一个在URL中未定义的参数(code)。

r.URL.Query().Get() 返回在URL地址中定义的参数。在您的情况下,您正在搜索缺失的code参数。

检查Exchange方法,它将授权码转换为令牌。

func (c *Config) Exchange(ctx context.Context, code string) (*Token, error).

在您的情况下,令牌是一个URL参数,但它没有声明。总之,请将令牌字符串作为参数包含在URL中,否则在代码的某个地方进行声明。

英文:

You are trying to access an url parameter (code) which is not defined in your url.

r.URL.Query().Get() returns an url parameter defined in the url address. In your case you are searching for code param, which is missing.

Checking the Exchange method, this converts an authorization code into a token.

func (c *Config) Exchange(ctx context.Context, code string) (*Token, error).

The token in your case is an url parameter, but it's not declared. To sum up please include the token string in the url as a parameter otherwise declare somewhere in your code.

huangapple
  • 本文由 发表于 2016年3月8日 14:48:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/35860811.html
匿名

发表评论

匿名网友

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

确定