英文:
Using Firebase Admin SDK for Go from AppEngine
问题
在编写一个使用AppEngine/Go后端的应用程序时,我想要验证Firebase ID令牌(JWT),但在AppEngine上运行时遇到了以下问题:
在App Engine中,无法使用http.DefaultTransport和http.DefaultClient。请参阅https://cloud.google.com/appengine/docs/go/urlfetch/
Admin SDK的说明在这里:https://firebase.google.com/docs/admin/setup
以下代码可能会解决问题,前提是client.ks是一个可导出的属性,因此可以从使用该库的应用程序中进行写入:
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
}
// 通过使用urlfetch客户端覆盖HTTP客户端
client.ks.HTTPClient = urlfetch.Client(ctx)
我的选择是否仅限于a)分叉并手动添加对urlfetch的支持,b)寻找除了看似官方的解决方案之外的其他解决方案..
编辑:根据Gavin的建议,我尝试将其更改为以下内容:
// 使用AppEngine的urlfetch覆盖默认的HTTP客户端
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...)
而在crypto.go
文件的refreshKeys()
方法中执行的实际HTTP通信并不使用这个http.Client;相反,它尝试使用在httpKeySource
中设置的HTTPClient属性。在我看来,这个属性从未在任何地方设置过,因此它总是默认为http.DefaultClient
。
英文:
while writing a AppEngine/Go backend that would want to verify firebase id tokens (jwt) I ran across this problem running it on AppEngine:
http.DefaultTransport and http.DefaultClient are not available in App Engine. See https://cloud.google.com/appengine/docs/go/urlfetch/
The Admin SDK is described here: https://firebase.google.com/docs/admin/setup
The following might fix it, only if client.ks was an exported property and thus writable from the app using the 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)
Are my options limited to a) forking this and manually adding support for urlfetch b) looking for another solutions besides the seemingly OFFICIAL one..
EDIT: As suggested by Gavin, I tried changing this to the following:
// 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
}
This, however, doesn't do the trick. As far as I can see (having done some source diving in the said library "firebase.google.com/go"
), the http.Client passed in through the options.Client is only used for Creds creation:
creds, err := transport.Creds(ctx, o...)
And the actual HTTP communication done in method refreshKeys()
of crypto.go
does not use this; instead, it attempts to use the HTTPClient property set in the httpKeySource
. IMO this is never set anywhere, and therefore it always defaults to the http.DefaultClient
.
答案1
得分: 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)
...
}
英文:
When creating a new application with the Firebase Admin SDK, you can pass in options from the "google.golang.org/api/option" package. The option you want to pass in is 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)
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论