“无法使用软件包”;golang.org/x/oauth2“;要通过facebook验证,请执行以下操作:;缺少重定向“uri参数”;
此代码适用于:“无法使用软件包”;golang.org/x/oauth2“;要通过facebook验证,请执行以下操作:;缺少重定向“uri参数”;,facebook,go,oauth-2.0,google-oauth,Facebook,Go,Oauth 2.0,Google Oauth,此代码适用于: 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) h
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()失败,原因是“oauth2:无法获取令牌:400错误”
请求-响应:{“错误”:{“消息”:“缺少重定向uri”
参数“,”类型“:”OAutheException“,”代码“:191,“fbtrace_id:”XXXX“}”
包golang.org/x/oauth2
是否无法将代码
交换为令牌
?我找到了丢失的内容。显然,我需要在oauthConfig
struct中添加RedirectURL
字段,以使Exchange()
正常工作。这不是Slack或GitHub的情况,但显然FB有点挑剔
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,
}