【问题标题】:Convert Google OAuth id token into Firebase ID token in React在 React 中将 Google OAuth id 令牌转换为 Firebase ID 令牌
【发布时间】:2020-11-12 10:40:44
【问题描述】:

我正在使用 Firebase 身份验证进行登录和注册。我正在尝试集成 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,我不确定那是什么。

【问题讨论】:

    标签: reactjs firebase rest express firebase-authentication


    【解决方案1】:

    您需要传递以下数据:

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

    【讨论】:

    • 当我将“localhost:3000”作为 requestURI 时,出现错误“MISSING_REQUEST_URI”
    • 是的,我也添加了方案
    • 好的,所以我只使用了 react-google-login 库并且能够跳过调用 API。谢谢您的帮助。你认为你可以支持我的问题吗?
    猜你喜欢
    • 2021-09-07
    • 2020-01-10
    • 2014-05-12
    • 2013-08-17
    • 2017-01-24
    • 2018-05-28
    • 2016-12-27
    • 1970-01-01
    • 2017-10-23
    相关资源
    最近更新 更多