2016-04-09 5 views
0

私の使用例では、GoogleのoauthリダイレクトURLにクエリパラメータを追加する必要があります。私はredirectというキーを持つクエリパラメータを追加しています。私は次のように追加しようとしていますgolangのgoogle oauthにクエリパラメータを追加するにはどうすればよいですか?

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の状態パラメタにリダイレクトパラメータを追加しています。したがって、私が状態コード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 
} 

?区切り文字を使用して文字列を分割して状態値を取得できます。しかし、私はGoogleのoauthでURLをリダイレクトするためにクエリのパラメータを追加する標準的な方法が必要であると思った。誰かがこれについていくつかの提案をしてもらえますか?

答えて

0

あなたは近いと思います。これが私の仕事:

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

authUrl := oAuthConfig.AuthCodeURL("state", 
    oauth2.AccessTypeOffline, 
    hostDomainOption) 

私はあなたがAuthCodeURL方法がvariadicであることに気付いている立ち往生されているかもしれない場所だと思います。

関連する問題