Redirect 如何在golang中向google oauth添加查询参数?

Redirect 如何在golang中向google oauth添加查询参数?,redirect,go,oauth,oauth-2.0,google-oauth,Redirect,Go,Oauth,Oauth 2.0,Google Oauth,在我的用例中,我必须向GoogleOAuth重定向URL添加一个查询参数。我正在添加一个查询参数,其键为redirect。我想用下面的方式来补充 var ( googleRedirectURL = "http://127.0.0.1:8080/oauth-callback/google" oauthCfg = &oauth2.Config{ ClientID: "XXXXXXXXXX", ClientSecret: "XXXXXXX

在我的用例中,我必须向GoogleOAuth重定向URL添加一个查询参数。我正在添加一个查询参数,其键为
redirect
。我想用下面的方式来补充

var (
    googleRedirectURL = "http://127.0.0.1:8080/oauth-callback/google"
    oauthCfg = &oauth2.Config{
        ClientID:     "XXXXXXXXXX",
        ClientSecret: "XXXXXXXXXX",
        Endpoint:     google.Endpoint,
        RedirectURL:  "http://127.0.0.1:8080/oauth-callback/google",
        Scopes:       []string{"https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"},
    }
    //random string for oauth2 API calls to protect against CSRF
    googleOauthStateString = getUUID()
)

const profileInfoURL = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json"

func HandleGoogleLogin(w http.ResponseWriter, r *http.Request) {
    redirect := strings.TrimSpace(r.FormValue("redirect"))
    if redirect == "" {
        httpErrorf(w, "HandleGoogleLogin() :: Missing redirect value for /login")
        return
    }
    q := url.Values{
        "redirect": {redirect},
    }.Encode()

    //params := '{"redirect": '+redirect+'}'
    log.Printf("HandleGoogleLogin() :: redirect %s ", q)

    //param     := oauth2.SetAuthURLParam("redirect", q)
    // url  := oauthCfg.AuthCodeURL("state", param)

    //append the redirect URL to the request
    oauthCfg.RedirectURL = googleRedirectURL
    url := oauthCfg.AuthCodeURL("state")
    url = oauthCfg.AuthCodeURL(googleOauthStateString, oauth2.AccessTypeOnline)
    url = url + "?redirct=" + q
    http.Redirect(w, r, url, http.StatusTemporaryRedirect)
}
但这是将重定向参数附加到url的状态参数。因此,当我比较state code
oauthCfg.AuthCodeURL(“state”)
时,值不同。我指的是下面的支票

state := r.FormValue("state")
log.Printf("HandleGoogleCallback() :: state string %s ", state)
if state != googleOauthStateString {
    log.Printf("invalid oauth state, expected '%s', got '%s'\n", googleOauthStateString, state)
    http.Redirect(w, r, "/", http.StatusTemporaryRedirect)
    return
}

我可以使用
分隔符拆分字符串以获取状态值。但我认为在GoogleOAuth中必须有一种添加查询参数以重定向url的标准方法。有人能给我一些建议吗?

我想你很接近了。这对我很有用:

hostDomainOption := oauth2.SetAuthURLParam("hd", "example.com")

authUrl := oAuthConfig.AuthCodeURL("state",
    oauth2.AccessTypeOffline,
    hostDomainOption)
我想你可能会被卡住的地方是注意到方法是错误的