英文:
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("-------------------")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论