在React中将Google OAuth ID令牌转换为Firebase ID令牌

时间:2020-07-22 19:29:44

标签: reactjs firebase rest express firebase-authentication

我正在使用Firebase Auth进行登录和注册。我正在尝试集成Google登录。现在,我已经获得了一个Google Oauth ID,需要将其转换为Firebase ID令牌,以便可以使用身份验证标头登录。

const FirebaseIdToken = `Bearer ${token}`;
  localStorage.setItem("FirebaseIdToken", FirebaseIdToken);
  API.defaults.headers.common["Authorization"] = FirebaseIdToken;

我正在跟踪以下链接:https://firebase.google.com/docs/reference/rest/auth/#section-sign-in-with-oauth-credential来获得发帖请求,但是我在这样做时遇到了麻烦。

  const data = {
    postBody: token, //Google Oauth token
  };

  API.post(
    `https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=[API-KEY]`,
    { data }
  ).then((res) => {
    console.log(res);
    console.log(res.data);
  });

我刚收到一个Post 400错误。该机构还要求一个requestUri,我不确定那是什么。

1 个答案:

答案 0 :(得分:1)

您需要传递以下数据:

const data = {
  returnSecureToken: true,
  requestUri: 'url-where-your-app-is-served-eg-http://localhost',
  postBody:`id_token=${token}&providerId=google.com`,
};