2016-04-09 10 views
0

Kullanım durumunda google oauth yönlendirme URL'sine bir sorgu parametresi eklemem gerekiyor. redirect anahtarıyla bir sorgu parametresi ekliyorum. Ben, şu şekildeGolang'da google oauth'a sorgu paramları nasıl eklenir?

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

eklemek çalışıyorum Ama bu url devlet param için yönlendirme param ekliyordur. Yani, oauthCfg.AuthCodeURL("state") durum kodunu karşılaştırdığımda, değer farklılık gösterir. Aşağıdaki kontrolü kastediyorum.

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 
} 

Ben ? ayırıcı kullanılarak devlet değeri elde etmek dize ayırabilirsiniz. Ancak, google oauth'da URL'yi yeniden yönlendirmek için sorgu parametresi eklemenin standart bir yolu olması gerektiğini düşündüm. Birisi bu konuda bazı önerilerde bulunabilir mi?

cevap

0

Sanırım yakınsınız. Bu benim için çalıştı:

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

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

Sana AuthCodeURL yöntem variadic olduğunu fark edilir sıkışmış olabileceğini nerede düşünüyorum.

İlgili konular