如何让 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 是什么?
doh2! http://godoc.org/github.com/crhym3/go-endpoints/endpoints#pkg-constants
最佳答案
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("-------------------")
}
关于google-app-engine - go-endpoints oauth2 用户始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20733872/