go-endpoints oauth2 用户始终为 nil

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

go-endpoints oauth2 user always nil

问题

如何使api explorer oauth2在go-endpoints中工作?(范围为https://www.googleapis.com/auth/userinfo.email)

func (gs *GreetingService) Store(r *http.Request, req *Request, resp *Response) error {
    c := appengine.NewContext(r)
    u := user.Current(c)
    if u != nil {c.Infof("Hello, %v\n", u)}
    if u != nil {fmt.Printf("Hello, %v\n", u)}
}

u始终为nil,在Java中,我必须添加类似以下内容才能使其工作。

@Api(name = "rest1",
     version = "0",
     scopes = {Id.EMAIL_SCOPE},
     clientIds = {Id.WEB_CLIENT_ID, Id.ANDROID_CLIENT_IDd, Id.ANDROID_CLIENT_IDr, Id.EXPLORER_ID},
     audiences = {Id.ANDROID_AUDIENCE})

如何使其在go-endpoints中工作?

编辑

哎呀!我找到了这个链接http://godoc.org/github.com/crhym3/go-endpoints/endpoints#CurrentBearerTokenUser PS golang的API_EXPLORER_CLIENT_ID是什么?

哎呀2!
http://godoc.org/github.com/crhym3/go-endpoints/endpoints#pkg-constants

英文:

How do you make the api explorer oauth2 work for go-endpoints? (scope https://www.googleapis.com/auth/userinfo.email )

func (gs *GreetingService) Store(r *http.Request, req *Request, resp *Response) error {
    c := appengine.NewContext(r)
    u := user.Current(c)
    if u != nil {c.Infof("Hello, %v\n", u)}
    if u != nil {fmt.Printf("Hello, %v\n", u)}
}

u is always nil, in java I had to add something like this to make it work.

@Api(name = "rest1",
     version = "0",
     scopes = {Id.EMAIL_SCOPE},
     clientIds = {Id.WEB_CLIENT_ID, Id.ANDROID_CLIENT_IDd, Id.ANDROID_CLIENT_IDr, Id.EXPLORER_ID},
     audiences = {Id.ANDROID_AUDIENCE})

How do you make it work for go-endpoints?

EDIT

doh! I found this http://godoc.org/github.com/crhym3/go-endpoints/endpoints#CurrentBearerTokenUser PS what is the API_EXPLORER_CLIENT_ID for golang?

doh2!
http://godoc.org/github.com/crhym3/go-endpoints/endpoints#pkg-constants

答案1

得分: 1

func (gs *GreetingService) Store(r *http.Request, req *Request, resp *Response) error {
c := endpoints.NewContext(r)

u, err := endpoints.CurrentBearerTokenUser(c, []string{"https://www.googleapis.com/auth/userinfo.email"}, []string{"...apps.googleusercontent.com", endpoints.ApiExplorerClientId})
if err != nil {return err}

c.Infof("-------------------")
if u != nil {c.Infof("Hello, %v\n", u)}
c.Infof("-------------------")

}

英文:
func (gs *GreetingService) Store(r *http.Request, req *Request, resp *Response) error {
    c := endpoints.NewContext(r)

    u, err := endpoints.CurrentBearerTokenUser(c, []string{"https://www.googleapis.com/auth/userinfo.email"}, []string{"...apps.googleusercontent.com",endpoints.ApiExplorerClientId})
    if err != nil {return err}

    c.Infof("-------------------")
    if u != nil {c.Infof("Hello, %v\n", u)}
    c.Infof("-------------------")
}

huangapple
  • 本文由 发表于 2013年12月23日 04:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/20733872.html
匿名

发表评论

匿名网友

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

确定