在编写想要验证firebase id令牌(jwt)的AppEngine / Go后端时,我遇到了在AppEngine上运行它的问题:
http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://cloud.google.com/appengine/docs/go/urlfetch/
此处描述了管理员SDK:https://firebase.google.com/docs/admin/setup
以下可能会解决此问题,仅当client.ks是导出的属性并因此可以使用lib从应用程序写入时:
client, err := app.Auth()
if err != nil {
log.Errorf(ctx, "Error getting auth client: %v", err)
writeJsonResponse(ctx, w, apiResponse{Message: "Firebase error"},
http.StatusInternalServerError)
return
}
// Override the HTTP client by using the urlfetch client to
// make it work under AppEngine
client.ks.HTTPClient = urlfetch.Client(ctx)
我的选择是否仅限于a)分叉并手动添加对urlfetch的支持b)寻找除了看似官方的其他解决方案之外的其他解决方案..:o
编辑:正如Gavin所建议的那样,我尝试将其更改为以下内容:
// Override the default HTTP client with AppEngine's urlfetch
opt := option.WithHTTPClient(urlfetch.Client(ctx))
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
log.Errorf(ctx, "Error initializing firebase app: %v", err)
writeJsonResponse(ctx, w, apiResponse{Message: "Firebase error"},
http.StatusInternalServerError)
return
}
然而,这并不能解决问题。据我所知(在所述库"firebase.google.com/go"
中完成了一些源头潜水),通过options.Client传入的http.Client仅用于创建Creds:
creds, err := transport.Creds(ctx, o...)
在refreshKeys()
的方法crypto.go
中完成的实际HTTP通信不使用此功能;相反,它尝试使用httpKeySource
中设置的HTTPClient属性。 IMO永远不会在任何地方设置,因此它始终默认为http.DefaultClient
。
答案 0 :(得分:1)
使用Firebase Admin SDK创建新应用程序时,您可以从“google.golang.org/api/option”包中传入选项。您要传入的选项是WithHTTPClient。
func handleRequest(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
opt := option.WithHTTPClient(urlfetch.Client(ctx))
app, err := firebase.NewApp(ctx, config, opt)
...
}