【问题标题】:firebase verify id token gives: Firebase ID token has incorrect "iss"firebase 验证 id 令牌给出:Firebase ID 令牌的“iss”不正确
【发布时间】:2018-05-28 18:17:40
【问题描述】:

如何在服务器上验证从客户端发送的idToken?

我正在使用 python 关注docs here。基本上我用email和password在客户端登录,返回idToken,然后我将这个idToken发送到服务器进行验证,但得到如下错误。

ValueError:Firebase ID 令牌的“iss”(颁发者)声明不正确。 预期为“https://securetoken.google.com/myproject”但得到 “https://identitytoolkit.google.com/”。确保 ID 令牌来了 来自与服务帐号相同的 Firebase 项目 验证此 SDK。看 https://firebase.google.com/docs/auth/admin/verify-id-tokens 为 有关如何检索 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)

【问题讨论】:

    标签: python firebase firebase-authentication


    【解决方案1】:

    除了发送邮箱和密码外,你还需要将标志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}'
    

    Documentation

    【讨论】:

      【解决方案2】:

      看来我在客户端遵循了错误的文档;最终我关注this docs,并使用以下代码提取了我发送到后端进行验证的idToken:

      firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {
        // Send token to your backend via HTTPS
        // ...
      }).catch(function(error) {
        // Handle error
      });
      

      【讨论】:

        猜你喜欢
        • 2017-01-24
        • 1970-01-01
        • 2021-12-04
        • 2019-04-09
        • 2018-07-23
        • 2021-05-04
        • 2020-02-10
        • 2019-02-26
        • 2019-06-28
        相关资源
        最近更新 更多