如何验证从服务器上的客户端发送的idToken
?
我使用Python关注此处的文档。基本上,我使用返回idToken
的email
和password
在客户端上签名,然后将此idToken
发送到服务器以进行验证,但在下面有错误。
valueerror:firebase id代币的" ISS"(发行人)索赔不正确。 期望" https://securetoken.google.com/myproject",但 " https://indistitytoolkit.google.com/"。确保ID令牌来了 来自与过去的服务帐户相同的壁炉项目 身份验证此SDK。看 https://firebase.google.com/docs/auth/admin/verify-id-tokens for 有关如何检索ID令牌的详细信息。
我很确定我与客户在同一项目中生成了服务帐户JSON。有人知道可能是什么问题吗?
我的服务器代码看起来像这样:
from firebase_admin import credentials, initialize_app, auth
def is_authenticated(id_token):
cred = credentials.Certificate("service-account.json")
app = initialize_app(cred)
return auth.verify_id_token(id_token, app)
除了发送电子邮件和密码外,您还必须将标志returnSecureToken
设置为 true
,否则您的idtoken无效,您也不会获得刷新令牌。
curl 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=[API_KEY]'
-H 'Content-Type: application/json'
--data-binary '{"email":"[user@example.com]","password":"[PASSWORD]","returnSecureToken":true}'
文档
看来我在客户端遵循错误的文档;最终,我遵循此文档,并使用以下代码提取我发送给后端进行验证的idToken
:
firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
// Send token to your backend via HTTPS
// ...
}).catch(function(error) {
// Handle error
});