刷新 firebase 令牌 Flutter 和 RESTfull API

Refresh firebase token Flutter and RESTfull API

如何刷新我从当前用户那里获得并放入 API 后的 firebase 令牌?

当您收到 firebase 令牌时,您也会收到响应 refresh_token 属性。您可以保存它,稍后将 POST 请求发送至:

https://securetoken.googleapis.com/v1/token?key=[API_KEY]

与 body:

grant_type: refresh_token

refresh_token: [refresh_token]

这里有更多详细信息: https://firebase.google.com/docs/reference/rest/auth#section-refresh-token

// Sign In with email and password
  var signInResponse = await http.post(
    Uri.parse(
        'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=[API_KEY]'),
    body: {
      "email": "example@example.com",
      "password": "example",
      "returnSecureToken": "true"
    },
  );

  var jsonSignInResponse =
      jsonDecode(signInResponse.body) as Map<String, dynamic>;
  
  // This refresh token you can use to refresh your auth token
  var refreshToken = jsonSignInResponse['refreshToken'];

  // Make suchlike request when you need to refresh your auth token
  var refreshResponse = await http.post(
    Uri.parse(
        'https://securetoken.googleapis.com/v1/token?key=[API_KEY]'),
    body: {
      "grant_type": "refresh_token",
      "refresh_token": refreshToken,
    },
  );

  // Here you get your refreshed auth token -> jsonRefreshResponse['id_token']
  var jsonRefreshResponse =
      jsonDecode(refreshResponse.body) as Map<String, dynamic>;