Unable to use package "golang.org/x/oauth2" to authenticate with facebook: "Missing redirect_uri parameter"

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

Unable to use package "golang.org/x/oauth2" to authenticate with facebook: "Missing redirect_uri parameter"

问题

这段代码是有效的:

func handleFacebookCallback(w http.ResponseWriter, r *http.Request) {
    state := r.FormValue("state")
    if state != oauthStateString {
        fmt.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state)
        http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
        return
    }

    code := r.FormValue("code")

    ////////////////////////////////////////////////////    
    Url, err := url.Parse(oauthConf.Endpoint.TokenURL)
    if err != nil {
        log.Fatal("Parse: ", err)
    }
    parameters := url.Values{}
    parameters.Add("client_id", oauthConf.ClientID)
    parameters.Add("client_secret", oauthConf.ClientSecret)
    parameters.Add("redirect_uri", "http://localhost:9090/oauth2callback")
    parameters.Add("code", code)
    Url.RawQuery = parameters.Encode()
    resp, err := http.Get(Url.String())

    if err != nil {
        fmt.Printf("Get: %s\n", err)
        http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
        return
    }
    defer resp.Body.Close()
}

但是,当我将标记//////...下面的部分替换为以下内容时:

token, err := oauthConf.Exchange(oauth2.NoContext, code)
if err != nil {
    fmt.Printf("oauthConf.Exchange() failed with '%s'\n", err)
    http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
    return
}

我得到的错误信息是:

oauthConf.Exchange() failed with 'oauth2: cannot fetch token: 400 Bad Request Response: {"error":{"message":"Missing redirect_uri parameter.","type":"OAuthException","code":191,"fbtrace_id":"XXXX"}}'

是否golang.org/x/oauth2包无法将code交换为token

英文:

This code works:

func handleFacebookCallback(w http.ResponseWriter, r *http.Request) {
	state := r.FormValue("state")
	if state != oauthStateString {
		fmt.Printf("invalid oauth state, expected '%s', got '%s'\n", oauthStateString, state)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

	code := r.FormValue("code")

////////////////////////////////////////////////////    
	Url, err := url.Parse(oauthConf.Endpoint.TokenURL)
	if err != nil {
		log.Fatal("Parse: ", err)
	}
	parameters := url.Values{}
	parameters.Add("client_id", oauthConf.ClientID)
	parameters.Add("client_secret", oauthConf.ClientSecret)
	parameters.Add("redirect_uri", "http://localhost:9090/oauth2callback")
	parameters.Add("code", code)
	Url.RawQuery = parameters.Encode()
	resp, err := http.Get(Url.String())

	if err != nil {
		fmt.Printf("Get: %s\n", err)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}
	defer resp.Body.Close()

But when I replace the part below the marker //////... with:

	token, err := oauthConf.Exchange(oauth2.NoContext, code)
	if err != nil {
		fmt.Printf("oauthConf.Exchange() failed with '%s'\n", err)
		http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
		return
	}

I get:

> oauthConf.Exchange() failed with 'oauth2: cannot fetch token: 400 Bad
> Request Response: {"error":{"message":"Missing redirect_uri
> parameter.","type":"OAuthException","code":191,"fbtrace_id":"XXXX"}}'

Is the package golang.org/x/oauth2 unable to exchange a code for a token?

答案1

得分: 1

我发现了缺少的部分。显然,我需要在oauthConfig结构体中添加RedirectURL字段,以使Exchange()正常工作。这对于Slack或GitHub来说并非如此,但显然Facebook要求更加严格。

var oauthConf = &oauth2.Config{
    ClientID:     "YOUR_CLIENT_ID",
    ClientSecret: "YOUR_CLIENT_SECRET",
    RedirectURL:  "http://localhost:9090/oauth2callback", /* 修正! */
    Scopes:       []string{"public_profile"},
    Endpoint:     facebook.Endpoint,
}
英文:

I found out what was missing. I apparently need to add the RedirectURLfield in the oauthConfig struct to get Exchange() to work properly. This is not the case for Slack or GitHub but apparently FB is slightly more picky.

var oauthConf = &oauth2.Config{
		ClientID:     "YOUR_CLIENT_ID",
		ClientSecret: "YOUR_CLIENT_SECRET",
		RedirectURL:  "http://localhost:9090/oauth2callback", /* Fixed! */
		Scopes:       []string{"public_profile"},
		Endpoint:     facebook.Endpoint,
	}

huangapple
  • 本文由 发表于 2016年4月17日 10:38:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/36672107.html
匿名

发表评论

匿名网友

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

确定