第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

go - oauth2 無法獲取令牌:錯誤的請求

go - oauth2 無法獲取令牌:錯誤的請求

Go
汪汪一只貓 2021-12-20 19:20:23
我編寫了一個回調(diào)處理程序來使用 Google 帳戶登錄:func GoogleCallbackHandler(w http.ResponseWriter, r *http.Request) {    conf:=&oauth2.Config{        ClientID:"700740834863-m4om9r91htn19htq2b6a05fu6vu4j7i5.apps.googleusercontent.com",        ClientSecret:"...-rB",        RedirectURL:"http://localhost:3000/auth/google/callback",        Scopes:[]string{"profile"},        Endpoint:google.Endpoint,    }    fmt.Println(conf.AuthCodeURL("state"))    code := r.URL.Query().Get("code")    token, err := conf.Exchange(oauth2.NoContext, code)    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }    client := conf.Client(oauth2.NoContext, token)    resp, err := client.Get("https://www.googleapis.com/userinfo/v2/me")    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }    raw, err := ioutil.ReadAll(resp.Body)    defer resp.Body.Close()    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }    var profile map[string]interface{}    if err := json.Unmarshal(raw, &profile); err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }    session, _ := util.GlobalSessions.SessionStart(w, r)    defer session.SessionRelease(w)    session.Set("id_token", token.Extra("id_token"))    session.Set("access_token", token.AccessToken)    session.Set("profile", profile)    // // Redirect to logged in page    http.Redirect(w, r, "/user", http.StatusMovedPermanently)}在main,我為處理程序提供服務(wù)http.HandleFunc("/",route.IndexHandler)http.HandleFunc("/auth/google/",route.GoogleCallbackHandler)http.Handle("/user",negroni.New(    negroni.HandlerFunc(route.IsAuthenticated),    negroni.Wrap(http.HandlerFunc(route.UserHandler)),))上次問這個問題后,我設(shè)法用上面的代碼修復(fù)它,成功訪問和獲取數(shù)據(jù),但昨晚它突然再次出現(xiàn)此錯誤,我不明白為什么它在最近幾天工作后不再起作用使用完全相同的代碼我怎樣才能得到codefrom AuthCodeURL?有沒有辦法在不重定向到任何其他處理程序的情況下獲取用于交換的代碼?我想在一個這個處理程序中處理所有事情
查看完整描述

1 回答

?
回首憶惘然

TA貢獻(xiàn)1847條經(jīng)驗 獲得超11個贊

我可以通過添加一個處理程序來解決它


var (

    conf *oauth2.Config

)


func GoogleCallbackHandler(w http.ResponseWriter, r *http.Request) {

    conf=&oauth2.Config{

        ClientID:"700740834863-m4om9r91htn19htq2b6a05fu6vu4j7i5.apps.googleusercontent.com",

        ClientSecret:"...-rB",

        RedirectURL:"http://localhost:3000/auth/google/callback",

        Scopes:[]string{"profile"},

        Endpoint:google.Endpoint,

    }


    http.Redirect(w, r, conf.AuthCodeURL("state"), http.StatusMovedPermanently)

}


func GoogleLoginHandler(w http.ResponseWriter, r *http.Request) {

    code := r.URL.Query().Get("code")


    token, err := conf.Exchange(oauth2.NoContext, code)

    if err != nil {

        http.Error(w, err.Error(), http.StatusInternalServerError)

        return

    }


    client := conf.Client(oauth2.NoContext, token)

    resp, err := client.Get("https://www.googleapis.com/userinfo/v2/me")

    if err != nil {

        http.Error(w, err.Error(), http.StatusInternalServerError)

        return

    }


    raw, err := ioutil.ReadAll(resp.Body)

    defer resp.Body.Close()

    if err != nil {

        http.Error(w, err.Error(), http.StatusInternalServerError)

        return

    }


    var profile map[string]interface{}

    if err := json.Unmarshal(raw, &profile); err != nil {

        http.Error(w, err.Error(), http.StatusInternalServerError)

        return

    }


    session, _ := util.GlobalSessions.SessionStart(w, r)

    defer session.SessionRelease(w)


    session.Set("id_token", token.Extra("id_token"))

    session.Set("access_token", token.AccessToken)

    session.Set("profile", profile)


    http.Redirect(w, r, "/user", http.StatusMovedPermanently)

}


查看完整回答
反對 回復(fù) 2021-12-20
  • 1 回答
  • 0 關(guān)注
  • 221 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號