2

我正在尝试实现会话处理并将其与 go-endpoints 包结合起来!

我用来处理会话的包是 Gorilla Sessions (github.com/gorilla/sessions),我需要一些帮助..

我能够将 cookie 存储到客户端 .. 并且当我调用端点时可以看到 cookie 已发送到服务器。

当我尝试在调用 api 时从 Session 存储中获取 Session 值时的问题,我不能被扔到 cookie 中。它接缝的是端点包从额外的内容或其他东西中剥离了 http.Request 。

我尝试获取 cookie 的地方在 Server.go 中

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request){

     var store = sessions.NewCookieStore([]byte("secret123"));

     session, _ := store.Get(r, "session-name");

     // Get the previously flashes, if any.

     c.Infof("foo value is : %v",r.Cookies());

     if flashes := session.Flashes(); len(flashes) > 0 {

     // Just print the flash values.

     c.Infof("this is the testing post message with cookie from the ServeHTTP    : 
      %v",flashes);
      } 

      else {

      // Set a new flash.

      session.AddFlash("Hello, flash messages world!")

      c.Infof("No flashes found.");

      }

      session.Save(r, w)

      }

我得到的是一个空数组.... :(

有人有线索吗?

谢谢 !!!!!

4

1 回答 1

1

好吧,我猜我对 go-endpoints 的想法是错误的。我对 golang 很陌生(〜年)..

我想写一些关于我发现的东西以及如何保护我的 api 的东西。

第一步将遵循 go-endpoints 包说明,了解如何在以下位置注册和发现 api:https ://github.com/GoogleCloudPlatform/go-endpoints ,该包是最接近谷歌应用引擎端点的包,使用Java 或 Python ..

现在,假设 api 在线且可发现。如果我们不使用 oauth2 来保护 api,它们将被发现并授予所有用户访问权限 .. 并且我只想在我的公共 api 中而不是在我的私人 api 中批准一些事情.. 所以我尝试了 gorilla session 认为它会解决我的问题 ..

我所做的是尝试通过用中间件包装所有通过“/_ah/api/....”的 rout 调用来收听传入的 api 调用,你能想象.. 花了我很长时间才明白这条路径是保留给 google api而且我可以做我正在尝试的事情..最终..我明白了..击球手后来永远...

言归正传,在公开 api 的名称之后,您应该使用 info.ClientIds、info.Scopes。

代码示例---->

const (
dummyClientID = "google appengine client id" 
dummyScope1   = "https://www.googleapis.com/auth/plus.login"
dummyScope2   = "https://www.googleapis.com/auth/plus.me"
dummyScope3   = "https://www.googleapis.com/auth/userinfo.email"
dummyScope4   = "https://www.googleapis.com/auth/userinfo.profile"
dummyAudience = "people"
)

var (
emptySlice = []string{}
clientIDs  = []string{dummyClientID}  // this is the clientId of the project
scopes     = []string{dummyScope1,dummyScope2,dummyScope3,dummyScope4} // >this are the req oauth2 scopes that the user hase to approve.
audiences  = []string{dummyAudience} // this is only for android !
)


info := manageApi.MethodByName("GetBusinessById").Info()
info.Name, info.HTTPMethod, info.Path, info.Desc = "GetBusinessById",   >"POST","GetBusinessById", "Get the business if bid is sent."
info.ClientIds, info.Scopes = clientIDs, scopes  

现在剩下要做的就是在 api 函数中创建一个 endpoint.NewContext 并要求适当的范围来获取 user.User ..

 func (ms *ManageService) GetBusinessById(r *http.Request, req >*JsonInGetBusinessById, resp *JsonOutEditBusiness) error {
 // go get the business by bid.
 DalInst := ManageDataAccessLayer.DALManagerFactory()

 context := endpoints.NewContext(r)

 u,err := >context.CurrentOAuthUser("https://www.googleapis.com/auth/userinfo.email")
 if err != nil {
     return err
 }else {

   var businessObj = DalInst.GetBusinessByBid(context, req.BidStr)


  resp.BidStr = u.Email //just for testing to see if the client is auth and >we can get client Email..

   resp.NameStr = businessObj.NameStr
   resp.AddressStr = businessObj.AddressStr
   resp.DescriptionStr = businessObj.DescriptionStr
   resp.DescriptionTwo = businessObj.DescriptionTwo
   resp.PhoneNumberStr = businessObj.PhoneNumberStr

   return nil

}

好的..希望我说清楚了一些事情!

于 2015-03-24T14:18:36.957 回答