How to add query params to google oauth in golang?

In my use case I have to add a query param to google oauth redirect URL. I am adding a query param with key as redirect. I am trying to add in the following way,

var (
    googleRedirectURL = ""
    oauthCfg = &oauth2.Config{
        ClientID:     "XXXXXXXXXX",
        ClientSecret: "XXXXXXXXXX",
        Endpoint:     google.Endpoint,
        RedirectURL:  "",
        Scopes:       []string{"", ""},
    //random string for oauth2 API calls to protect against CSRF
    googleOauthStateString = getUUID()

const profileInfoURL = ""

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

    //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)

But this is appending the redirect param to the state param of the url. So when I compare the state code oauthCfg.AuthCodeURL("state") the value differs. I mean the following check.

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)

I can split the string to get the state value using ? delimiter. But I thought there must be a standard way of adding query param to redirect url in google oauth. Could someone give some suggestions on this?


I think you're close. This worked for me:

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

authUrl := oAuthConfig.AuthCodeURL("state",

I think where you might have been stuck is noticing that the AuthCodeURL method is variadic.

Need Your Help

puma hot restarting on_restart function: necessary for Rails apps?

ruby-on-rails-3 puma

In the configuration file example for Puma, it says the following for the on_restart function:

How to cast a Fraction object to Double object?

java casting

When I'm studying Android, I can cast an object type to other type, like this: