微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Firebase 管理员返回无效 ID 令牌错误

如何解决Firebase 管理员返回无效 ID 令牌错误

我正在尝试验证来自客户端的 id 令牌。

https://firebase.google.com/docs/auth/admin/verify-id-tokens#python

这是我的javascript代码

.....
firebase.auth()
    .signInWithPopup(provider)
    .then((result) => {
        firebase.auth().currentUser.getIdToken(/* forceRefresh */ true).then(function(idToken) {

            console.log(idToken);

            // https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
            fetch("http://localhost:5000/api/auth/verify",{
                method: 'POST',mode: 'same-origin',credentials: 'same-origin',headers: {
                    "Access-Control-Allow-Origin": "*",'Authorization': 'Bearer ' + idToken
                }
            })
.....

这是我的python代码

...
from firebase_admin import auth as firebase_auth
from ..api_config import * # My Firebase App and Firestore
...
try:
   ...
   decoded_token = firebase_auth.verify_id_token(Token,check_revoked=True)
   ...
except:
   ...

这是我得到的错误

[2021-06-27 13:03:10,166] ERROR in app: Exception on /api/auth/verify [POST]
Traceback (most recent call last):
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\google\auth\jwt.py",line 123,in _decode_jwt_segment      
    return json.loads(section_bytes.decode("utf-8"))
UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 1-2: invalid continuation byte

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\firebase_admin\_token_gen.py",line 408,in _decode_unverified
    header = jwt.decode_header(token)
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\google\auth\jwt.py",line 171,in decode_header
    header,_,_ = _unverified_decode(token)
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\google\auth\jwt.py",line 152,in _unverified_decode       
    header = _decode_jwt_segment(encoded_header)
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\google\auth\jwt.py",line 126,in _decode_jwt_segment      
    six.raise_from(new_exc,caught_exc)
  File "<string>",line 3,in raise_from
ValueError: Can't parse segment: b'\x05\xe6\xabz\xb7\xb2&\x16\xc6r#\xa2%%3#Sb"\xc2&\xb6\x96B#\xa2#\x83\x86Fc\x133\x83\x036#sC3cc\x13\x16\x16CC\x83\x13Cf\x13\x86F\x13\x16#\x03s\x13\x83ffC\x16S\x92"\xc2\'G\x97\x02#\xa2$\xa5uB\'\xd0'

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\flask\app.py",line 2070,in wsgi_app
    response = self.full_dispatch_request()
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\flask\app.py",line 1515,in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\flask\app.py",line 1513,in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\flask\app.py",line 1499,in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "C:\Users\hp\Documents\-----------------\Projects\Random-Manga-Stuffs\app\api\components\auth.py",line 16,in verify_user
    decoded_token = firebase_auth.verify_id_token(Token,check_revoked=True)
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\firebase_admin\auth.py",line 213,in verify_id_token      
    return client.verify_id_token(id_token,check_revoked=check_revoked)
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\firebase_admin\_auth_client.py",line 124,in verify_id_token
    verified_claims = self._token_verifier.verify_id_token(id_token)
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\firebase_admin\_token_gen.py",line 293,in verify_id_token    return self.id_token_verifier.verify(id_token,self.request)
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\firebase_admin\_token_gen.py",line 331,in verify
    header,payload = self._decode_unverified(token)
  File "c:\users\hp\documents\-----------------\projects\random-manga-stuffs\venv\lib\site-packages\firebase_admin\_token_gen.py",line 412,in _decode_unverified
    raise self._invalid_token_error(str(error),cause=error)
firebase_admin._auth_utils.InvalidIdTokenError: Can't parse segment: b'\x05\xe6\xabz\xb7\xb2&\x16\xc6r#\xa2%%3#Sb"\xc2&\xb6\x96B#\xa2#\x83\x86Fc\x133\x83\x036#sC3cc\x13\x16\x16CC\x83\x13Cf\x13\x86F\x13\x16#\x03s\x13\x83ffC\x16S\x92"\xc2\'G\x97\x02#\xa2$\xa5uB\'\xd0'
500 Internal Server Error: The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

我是这个 jwt 的新手,我有我的 key.json 服务帐户文件。我尝试登录退出我的不同谷歌帐户,但结果仍然相同

我的代码有问题吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?