Javascript Firebase Admin SDK GO VerifyIDToken返回的不是应用程序引擎上下文错误
我有一个用户通过了Firebase的身份验证,我想做的是在后端(Google cloud platform/Go)对该用户进行身份验证。我跟着文件走了 我在前端获取idToken,并使用以下代码将标头上的令牌发送到本地主机上运行的服务器Javascript Firebase Admin SDK GO VerifyIDToken返回的不是应用程序引擎上下文错误,javascript,firebase,google-app-engine,go,firebase-authentication,Javascript,Firebase,Google App Engine,Go,Firebase Authentication,我有一个用户通过了Firebase的身份验证,我想做的是在后端(Google cloud platform/Go)对该用户进行身份验证。我跟着文件走了 我在前端获取idToken,并使用以下代码将标头上的令牌发送到本地主机上运行的服务器 idToken = firebase.auth().currentUser.getIdToken() axios({ method: 'POST', url: 'https://localhost:8080/users', headers
idToken = firebase.auth().currentUser.getIdToken()
axios({
method: 'POST',
url: 'https://localhost:8080/users',
headers: {
'Authentication-Token': idToken
},
data: {
name: 'My name',
user_name: 'my_user_name',
email: 'example@example.com'
}
})
opt := option.WithCredentialsFile("firebase_credential.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
client, err := app.Auth(context.Background())
在后端,我想验证idToken。我首先使用以下代码创建auth客户机
idToken = firebase.auth().currentUser.getIdToken()
axios({
method: 'POST',
url: 'https://localhost:8080/users',
headers: {
'Authentication-Token': idToken
},
data: {
name: 'My name',
user_name: 'my_user_name',
email: 'example@example.com'
}
})
opt := option.WithCredentialsFile("firebase_credential.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
client, err := app.Auth(context.Background())
通过客户端和前端的idToken,我尝试了下面的代码
t, err := client.VerifyIDToken(context.Background(), idToken)
这怎么会造成一个错误呢
{"message":"Get https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com: oauth2: cannot fetch token: Post https://accounts.google.com/o/oauth2/token: not an App Engine context"}
我还试图curl
向服务器发送相同的请求,但出现了相同的错误
curl -X POST -H "Authentication-Token:hogehogemytoken" -d '{"name":"My name","user_name":"my user name","email":"example@example.com"}' http://localhost:8080/users
我确信我寄的是正确的代币。
有什么想法吗?似乎在appengine标准环境中我不得不使用
ctx := appengine.NewContext(r)
而不是
ctx := context.Background()
在appengine请求上下文中,必须处理更复杂的事情,因为它们是API请求上下文。另一方面,generalcontext.Background()
用于一般用途,因此与传入的Http请求无关
因此,我的解决方案是在init.go中传递appengine.NewContext(r)
,如下所示
r.POST("/users", func(c *gin.Context) { up.CreateUser(c, appengine.NewContext(c.Request)) })
将此appengine上下文传递给验证令牌
func (c authClient) VerifyToken(ctx context.Context, token string) (IDToken, error) {
opt := option.WithCredentialsFile("firebasecredential.json")
app, err := firebase.NewApp(ctx, nil, opt)
if err != nil {
return nil, err
}
client, err := app.Auth(ctx)
if err != nil {
return nil, err
}
t, err := client.VerifyIDToken(ctx, token)
if err != nil {
return nil, err
}
return idToken{t}, nil
}
VerifyIDToken
不支持上下文,请检查
截至目前:
func (c *Client) VerifyIDToken(idToken string) (*Token, error)
您的评论中的错误指向未来请求(FR),这意味着它尚未实现,因此它不是一个bug
您可以使用does:
也许是图书馆的一个错误?