oauth2无法获取令牌:错误的请求 [英] oauth2 cannot fetch token: bad request

查看:3107
本文介绍了oauth2无法获取令牌:错误的请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个处理程序,以便在访问路径 / auth / google / callback 时,我尝试通过OAuth2使用Google帐户登录。处理程序是这样实现的:

pre $ $ $ $ $ $ $

$ b $ 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,
作用域:[] 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)
返回
}

fmt.Println(令牌)

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

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



当我访问该路径时,它在浏览器中抛出如下错误:

  oauth2:无法获取标记: 400错误请求
响应:{
错误:invalid_request,
error_description:缺少所需参数:代码
}

我错过了什么吗?请指示我正确访问OAuth2并从Google帐户获取标记和信息 您正试图访问url参数( code ),这个参数在你的url中没有定义。


$ b

r.URL。 Query().Get()返回在url地址中定义的url参数。在你的情况下,你正在寻找缺少的 code param,
$ b 检查 Exchange 方法,这会将授权码(c * Config)Exchange(ctx context.Context,代码字符串)(* Token,error) 。

您的情况中的标记是url参数,但未声明。总结起来,请将url中的标记字符串作为参数包含进去,否则在代码中声明。


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

解决方案

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.

这篇关于oauth2无法获取令牌:错误的请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆