Firebase ID令牌中的Firebase刷新令牌

时间:2020-01-30 23:57:14

标签: firebase rest curl flutter firebase-authentication

我知道Firebase ID令牌可以用来代表用户。但是令牌会在1小时后过期。 为了解决这个问题,您可以在此处使用REST命令对文档和本文How to use the Firebase refreshToken to reauthenticate?进行重新认证 https://firebase.google.com/docs/reference/rest/auth/

我无法使它正常工作。这是用于在logcat上打印令牌的Android Flutter代码

FirebaseAuth.instance;.currentUser().then((FirebaseUser user) {
    if (user != null) {
        user.getIdToken(refresh: true).then((IdTokenResult token) {
             dbg.log('firebase TOKEN ' + token.token );
             dbg.log('firebase TOKEN authtime ' + token.authTime.toIso8601String() );
             dbg.log('firebase TOKEN expiration ' + token.expirationTime.toIso8601String() );
             dbg.log('firebase TOKEN issued at ' + token.issuedAtTime.toIso8601String() );
        });
    }
}).catchError((e) {
    dbg.log('fetch user ERROR: ' + e.toString());
});

验证令牌是否有效,您可以将其添加到curl:

#!/bin/sh

TOKEN=<your token>

curl -H "Authorization: Bearer $TOKEN" \
https://firestore.googleapis.com/v1/projects/<your project>/databases/<your databases> | jq

这有效。但是,当我尝试获取刷新令牌时:

#!/bin/sh

TOKEN=<your token>
WEB_API_KEY=<your web api key>

REFRESH_URL=https://identitytoolkit.googleapis.com/v1/accounts:signInWithCustomToken?key=${WEB_API_KEY}

ARGS='{"token":"'${TOKEN}'","returnSecureToken":true}'

echo $ARGS | jq

curl ${REFRESH_URL}  \
-H 'Content-Type: application/json' \
--data-binary ${ARGS} | jq

我收到以下错误:

{
  "error": {
    "code": 400,
    "message": "INVALID_CUSTOM_TOKEN",
    "errors": [
      {
        "message": "INVALID_CUSTOM_TOKEN",
        "domain": "global",
        "reason": "invalid"
      }
    ]
  }
}

我错过了一步吗?

0 个答案:

没有答案