好吧,我猜我对go-endpoints的想法是错误的..我对golang(〜year)很陌生。
我想写一些关于我发现的东西以及如何保护我的api的东西。
第一步将按照go-
endpoints软件包说明进行操作,有关如何注册和发现api的信息:https : //github.com/GoogleCloudPlatform/go-
endpoints,此软件包是使用google
app引擎终结点使用的最接近的软件包Java或Python ..
现在,可以说该api在线且可发现。如果我们不使用oauth2来保护api的安全,那么它们将是可发现的并授予所有用户访问权限..而且我只想在我的公共api中而不是在我的私有..中批准某些内容,因此我尝试了大猩猩会话,认为它将解决我的问题
..
我所做的是通过与中间件一起包装所有传递“ / _ah / api /
....”的路由调用来监听传入的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> >> }好的..希望我弄清楚一些事情!



