Martini oauth2callback在适应GAE后重定向到oauth2error。

huangapple go评论76阅读模式
英文:

Martini oauth2callback redirects to oauth2error after adapting to GAE

问题

以下是翻译好的内容:

package testapp

import (
    "github.com/go-martini/martini"
    goauth2 "github.com/golang/oauth2"
    "github.com/martini-contrib/oauth2"
    "github.com/martini-contrib/sessions"
    "net/http"
)

func init() {
    m := martini.Classic()
    m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
    m.Use(oauth2.Facebook(
        goauth2.Client("AppID", "AppSecret"),
        goauth2.RedirectURL("http://testapp.com/oauth2callback"),
        goauth2.Scope("email,user_birthday,user_friends,publish_actions,user_location,user_photos"),
    ))
    // Tokens are injected to the handlers
    m.Get("/", func(tokens oauth2.Tokens) string {
        if tokens.Expired() {
            return "not logged in, or the access token is expired"
        }
        return "logged in"
    })

    // Routes that require a logged in user
    // can be protected with oauth2.LoginRequired handler.
    // If the user is not authenticated, they will be
    // redirected to the login path.
    m.Get("/restrict", oauth2.LoginRequired, func(tokens oauth2.Tokens) string {
        return tokens.Access()
    })

    http.Handle("/", m)
}

以上是在本地服务器上完美运行的代码,但是当适应Google App Engine(将func main更改为init,将包名从main更改为testapp)后,oauth2callback请求不再工作,下面的请求被重定向到oauth2error处理程序。

http://testapp.com/oauth2callback?code=OAUTHRESPONSEFROMFACEBOOK&state=%2F

英文:

Code below works perfectly on local server, but when adapted to Google app engine(func main is changed to init and package name is set from main to test app) oauth2callback request is not working anymore, below request is redirected to oauth2error handler.

http://testapp.com/oauth2callback?code=OAUTHRESPONSEFROMFACEBOOK&state=%2F

package testapp

import (
    "github.com/go-martini/martini"
    goauth2 "github.com/golang/oauth2"
    "github.com/martini-contrib/oauth2"
    "github.com/martini-contrib/sessions"
    "net/http"
)

func init() {
    m := martini.Classic()
    m.Use(sessions.Sessions("my_session", sessions.NewCookieStore([]byte("secret123"))))
    m.Use(oauth2.Facebook(
        goauth2.Client("AppID", "AppSecret"),
        goauth2.RedirectURL("http://testapp.com/oauth2callback"),
        goauth2.Scope("email,user_birthday,user_friends,publish_actions,user_location,user_photos"),
    ))
    // Tokens are injected to the handlers
    m.Get("/", func(tokens oauth2.Tokens) string {
        if tokens.Expired() {
            return "not logged in, or the access token is expired"
        }
        return "logged in"
    })

    // Routes that require a logged in user
    // can be protected with oauth2.LoginRequired handler.
    // If the user is not authenticated, they will be
    // redirected to the login path.
    m.Get("/restrict", oauth2.LoginRequired, func(tokens oauth2.Tokens) string {
        return tokens.Access()
    })

    http.Handle("/", m)
}

答案1

得分: 0

我猜你需要在Get方法中注入App Engine上下文。

根据Go Martini仓库上的这个问题,你可以按照以下方式操作:

func AppEngine(m martini.Context, r *http.Request) {
    m.MapTo(appengine.NewContext(r), (*appengine.Context)(nil))
}

func init() {
    m := martini.Classic()
    m.Use(AppEngine)

    // ...

    m.Get("/", func(tokens oauth2.Tokens, c appengine.Context) string {
        if tokens.Expired() {
            return "未登录或访问令牌已过期"
        }
        return "已登录"
    })

    m.Get("/restrict", oauth2.LoginRequired, func(tokens oauth2.Tokens, c appengine.Context) string {
        return tokens.Access()
    })
}
英文:

My guess is that you need to inject App Engine context into the Get method.

According to this issue on Go Martini repo, you can do as follow:

func AppEngine(m martini.Context, r *http.Request) {
    m.MapTo(appengine.NewContext(r), (*appengine.Context)(nil))
}

func init() {
    m := martini.Classic()
    m.Use(AppEngine)

    // ...

    m.Get("/", func(tokens oauth2.Tokens, c appengine.Context) string {
        if tokens.Expired() {
            return "not logged in, or the access token is expired"
        }
        return "logged in"
    })

    m.Get("/restrict", oauth2.LoginRequired, func(tokens oauth2.Tokens, c appengine.Context) string {
        return tokens.Access()
    })
}

huangapple
  • 本文由 发表于 2014年12月13日 21:31:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/27459345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定