好吧,我猜我对 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
}
好的..希望我说清楚了一些事情!