Oauth2 Golang使用Framework Echo

时间:2019-06-15 12:10:06

标签: go go-echo

我只是学习Go,然后将Echo Golang用于我的Framework。然后我开始专门为Google学习Oauth2,但是出现了问题,我不知道是哪种错误

package controller

import (
    "fmt"
    "io/ioutil"
    "net/http"

    "github.com/labstack/echo"
    "github.com/spf13/viper"
    "golang.org/x/oauth2"
    "golang.org/x/oauth2/google"
)

//HELPER
var (
    googleOauthConfig *oauth2.Config
    oauthStateString  = "pseudo-random"
    oauthGoogleURLAPI = "https://www.googleapis.com/oauth2/v2/userinfo?access_token="
)

func initGoogleConfig() {
    googleOauthConfig = &oauth2.Config{
        RedirectURL:  "http://localhost:5000/api/v1/auth/google/callback",
        ClientID:     viper.GetString("env.GOOGLE_CLIENT_ID"),
        ClientSecret: viper.GetString("env.GOOGLE_CLIENT_SECRET"),
        Scopes:       []string{"https://www.googleapis.com/auth/userinfo.email"},
        Endpoint:     google.Endpoint,
    }
}

func getUserInfo(state string, code string) ([]byte, error) {
    if state != oauthStateString {
        return nil, fmt.Errorf("Invalid oauth state")
    }

    token, err := googleOauthConfig.Exchange(oauth2.NoContext, code)
    if err != nil {
        return nil, fmt.Errorf("Code exchange failed: %s", err.Error())
    }

    res, err := http.Get(oauthGoogleURLAPI + token.AccessToken)
    if err != nil {
        return nil, fmt.Errorf("failed get info: %s", err.Error())
    }

    defer res.Body.Close()
    contents, err := ioutil.ReadAll(res.Body)
    if err != nil {
        return nil, fmt.Errorf("Failed reading response body: %s", err.Error())
    }

    return contents, nil
}

//CONTROLLER

//TestAuth route
func TestAuth(c echo.Context) error {
    return c.String(http.StatusOK, "Test routing auth FROM GOOGLE indonesia!")
}

//GoogleLogin login with google
func GoogleLogin(c echo.Context) error {
    url := googleOauthConfig.AuthCodeURL(oauthStateString)
    return c.Redirect(http.StatusOK, url)
}

//GoogleCallback callback after login with google
func GoogleCallback(c echo.Context) error {
    content, err := getUserInfo(c.FormValue("state"), c.FormValue("code"))
    if err != nil {
        fmt.Println(err.Error())
        return c.Redirect(http.StatusOK, "/")
    }

    return c.String(http.StatusOK, string(content))
}

我得到这样的错误:

  

{“时间”:“ 2019-06-15T18:58:08.350201 + 07:00”,“级别”:“-”,“前缀”:“ echo”,“文件”:“ recover.go”, “ line”:“ 73”,“ message”:“ [PANIC RECOVER]运行时错误:无效的内存地址或nil指针取消引用goroutine 19 [正在运行]:\ ngithub.com/labstack/echo/middleware.RecoverWithConfig.func1.1.1( 0x15b4c38、0x1000、0xc000040000、0x16621e0、0xc0001aa120)\ n \ t / Users / rizaldinurmuhammad / go / src / github.com / labstack / echo / middleware / recover.go:71 + 0xf1 \ npanic(0x15115a0,0x19df3 t / usr / local / go / src / runtime / panic.go:522 + 0x1b5 \ ngolang.org/x/oauth2。(* Config).AuthCodeURL(0x0,0x1597bb7,0xd,0x0,0x0,0x0,0xfd56bea5,0x78019d9831db855f )\ n \ t / Users / rizaldinurmuhammad / go / src / golang.org / x / oauth2 / oauth2.go:152 + 0x56 \ ngo_papalla / api / controller.GoogleLogin(0x16621e0,0xc0001aa120,0x10,0x14e67e0)\ n \ t /Users/rizaldinurmuhammad/go/src/go_papalla/api/controller/auth.go:64 + 0x5a \ ngithub.com/labstack/echo。(* Echo).add.func1(0x16621e0,0xc0001aa120,0x159dd47,0x1b)\ n \ t / Users / rizaldinurmuhammad / go / src / github.com / labst ack / echo / echo.go:502 + 0x87 \ ngithub.com/labstack/echo/middleware.CORSWithConfig.func1.1(0x16621e0,0xc0001aa120,0x15b4c38,0x1000)\ n \ t / Users / rizaldinurmuhammad / go / src / github .com / labstack / echo / middleware / cors.go:121 + 0x478 \ ngithub.com/labstack/echo/middleware.RecoverWithConfig.func1.1(0x16621e0,0xc0001aa120,0x0,0x0)\ n \ t / Users / rizaldinurmuhammad / go / src / github.com / labstack / echo / middleware / recover.go:78 + 0xd5 \ ngithub.com/labstack/echo/middleware.LoggerWithConfig.func2.1(0x16621e0,0xc0001aa120,0x3,0x4)\ n \ t /Users/rizaldinurmuhammad/go/src/github.com/labstack/echo/middleware/logger.go:119 + 0x1c0 \ ngithub.com/labstack/echo。(* Echo).ServeHTTP(0xc00013c1c0,0x1653880,0xc0001b4000,0xc0001a6000) \ n \ t / Users / rizaldinurmuhammad / go / src / github.com / labstack / echo / echo.go:613 + 0x22d \ nnet / http.serverHandler.ServeHTTP(0xc0000c4b60、0x1653880、0xc0001b4000、0xc0001a6000)\ n \ t / usr / local / go / src / net / http / server.go:2774 + 0xa8 \ nnet / http。(* conn).serve(0xc00017e000,0x1654ac0,0xc00019c040)\ n \ t / usr / local / go / src /net/http/server.go:1878 + 0x851 \ n由net / http。(* Server)创建。Serve\ n \ t / usr / local / go / src / net / http / server.go:2884 + 0x2f4 \ n \ ngoroutine 1 [IO等待]:\ ninternal / poll.runtime_pollWait(0x5a15f08,0x72,0x0)\ n \ t / usr / local / go / src / runtime / netpoll.go:182 + 0x56 \ nternal / poll。( * pollDesc).wait(0xc000178018,0x72,0x0,0x0,0x15951ef)\ n \ t / usr / local / go / src / internal / poll / fd_poll_runtime.go:87 + 0x9b \ nternal / poll。(* pollDesc)。 waitRead(...)\ n \ t / usr / local / go / src / internal / poll / fd_poll_runtime.go:92 \ ninternal / poll。(* FD).Accept(0xc000178000,0x0,0x0,0x0,0x0, 0x0、0x0、0x0)\ n \ t / usr / local / go / src / internal / poll / fd_unix.go:384 + 0x1ba \ nnet。(* netFD).accept(0xc000178000,0x5a117b8,0x203000,0x203000)\ n \ t / usr / local / go / src / net / fd_unix.go:238 + 0x42 \ nnet。(* TCPListener).accept(0xc0000a8010,0xc0abaa7f7d,0xc000042800,0xc000149ba0)\ n \ t / usr / local / go / src /net/tcpsock_posix.go:139 + 0x32 \ nnet。(* TCPListener).AcceptTCP(0xc0000a8010,0x100000001,0xc000149bd0,0x104c108)\ n \ t / usr / local / go / src / net / tcpsock.go:247 + 0x48 \ ngithu b.com/labstack/echo.tcpKeepAliveListener.Accept(0xc0000a8010、0xc000149bf8、0x10b0996、0x5d04dd50、0x103696f)\n\t/Users/rizaldinurmuhammad/go/src/github.com/labstack/echo/echo2go:817 + \ nnet / http。(* Server).Serve(0xc0000c4b60,0x1652c40,0xc0000a8018,0x0,0x0)\ n \ t / usr / local / go / src / net / http / server.go:2859 + 0x22d \ ngithub.com /labstack/echo.(*Echo).StartServer(0xc00013c1c0,0xc0000c4b60,0x1,0xc0000b6ad8)\ n \ t / Users / rizaldinurmuhammad / go / src / github.com / labstack / echo / echo.go:704 + 0x388 \ ngithub .com / labstack / echo。(* Echo).Start(...)\ n \ t / Users / rizaldinurmuhammad / go / src / github.com / labstack / echo / echo.go:624 \ nmain.main() \ n \ t / Users / rizaldinurmuhammad / go / src / go_papalla / main.go:53 + 0x71b \ n \ ngoroutine 50 [IO等待]:\ ninternal / poll.runtime_pollWait(0x5a15d68、0x72、0xffffffffffffffff)\ n \ t / usr / local / go / src / runtime / netpoll.go:182 + 0x56 \ ninternal / poll。(* pollDesc).wait(0xc000182018,0x72,0x1000,0x1000,0xffffffffffffffffff)\ n \ t / usr / local / go / src / internal / poll / fd_poll_runtime.go:87 + 0x9b \ nternal / poll。(* p ollDesc).waitRead(...)\ n \ t / usr / local / go / src / internal / poll / fd_poll_runtime.go:92 \ nternal / poll。(* FD).Read(0xc000182000,0xc0001a0000,0x1000,0x1000 ,0x0,0x0,0x0)\ n \ t / usr / local / go / src / internal / poll / fd_unix.go:169 + 0x1f2 \ nnet。(* netFD).Read(0xc000182000,0xc0001a0000,0x1000,0x1000,0xc0000a6880 ,0xc000051938,0x12b7991)\ n \ t / usr / local / go / src / net / fd_unix.go:202 + 0x4f \ nnet。(* conn).Rea \ n“}       方法:GET,URI:/ api / v1 / auth / google,状态:500

请给个手势。

0 个答案:

没有答案