Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“无法使用软件包”;golang.org/x/oauth2“;要通过facebook验证,请执行以下操作:;缺少重定向“uri参数”;_Facebook_Go_Oauth 2.0_Google Oauth - Fatal编程技术网

“无法使用软件包”;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,
    }