【问题标题】:golang go-endpoints Session using gorilla-toolkit使用 gorilla-toolkit 的 golang go-endpoints 会话
【发布时间】:2015-05-22 07:27:07
【问题描述】:

我正在尝试实现会话处理并将其与 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)

      }

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

有人有线索吗?

谢谢!!!!!!

【问题讨论】:

  • 不要忽略错误。检查它在说什么。那,并且您的“商店”变量正在根据每个请求进行更新。它需要是全局的(简单的)或在应用程序范围的上下文中定义的。
  • 好的!我会尝试并发布更新!谢谢!

标签: session cookies go google-cloud-endpoints gorilla


【解决方案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 调用,你能想象.. 花了我很长时间才明白这条路径是保留的到谷歌 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

}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-03
    • 2014-03-18
    • 1970-01-01
    • 2022-01-20
    • 2015-06-13
    • 2016-05-07
    • 2021-04-09
    • 1970-01-01
    相关资源
    最近更新 更多