使用signInWithCredential()进行身份验证

Sas*_*sxa 5 firebase google-identity-toolkit firebase-authentication angularfire2

我正在尝试连接到第二个Firebase应用并进行身份验证signInWithCredential(),但我不知道如何对idToken第二个应用有效:

  connect(accessToken: string, config: FirebaseAppConfig) {
    let one: firebase.app.App = this.angularFireTwo.database["fbApp"];
    one.auth().currentUser.getToken()
      .then(idToken => firebase.auth.GoogleAuthProvider.credential(idToken, accessToken))
      .then(credential => {
        let two = firebase.initializeApp(config, `[${config.apiKey}]`);
        return two.auth().signInWithCredential(credential);
      })
      .catch(console.warn)
      .then(console.info);
  }
Run Code Online (Sandbox Code Playgroud)

我得到了错误https://www.googleapis.com/identitytoolkit/v3/:

IdP响应中的id_token无效

如果我使用signInWithPopup()我可以验证并且连接正常:

        two.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider())
Run Code Online (Sandbox Code Playgroud)

任何人都知道我应该怎么做才能获得有效的idToken?


更新:

我一直试图弄清楚身份验证过程,据我所知,它是这样的:

  1. 来自config: FirebaseAppConfigfirebase读取apiKeyauthDomain
  2. 它与服务器联系并获取已启用Google提供商的Web客户端ID123.apps.googleusercontent.com
  3. 用这个Web Client IDauthDomain它联系www.googleapis.com,它返回idToken
  4. idToken然后,这用于识别要求用户访问用户个人资料等的应用程序.
  5. 当用户同意时,回调将返回用于此身份验证的用户详细信息+凭据,其中包含idTokenWeb应用程序和accessToken用户

现在,如果我使用signInWithPopup()步骤2-3-4在后台完成(弹出窗口).我只需要一种idToken为步骤4 生成的方法,因此我可以使用它来生成凭证firebase.auth.GoogleAuthProvider.credential(idToken, accessToken)和登录signInWithCredential().

我可以访问登录第二个应用程序所需的所有内容 - 它是; apiKey,, authDomainWeb客户端ID 456.apps.googleusercontent.com和用户的唯一性accessToken.

但仍然无法弄清楚如何做到这一点.我在他们的身份验证配置中尝试了白名单应用程序的一个两个 Web客户端ID,希望这将允许他们接受彼此idToken的,但这不起作用......

boj*_*eil 6

当您致电:firebase.auth.GoogleAuthProvider.credential(idToken,accessToken))

第一个参数应为Google OAuth Id令牌.您正在使用Firebase Id令牌,这就是您收到错误的原因.此外,如果您已经登录,为什么还要使用signInWithCredential再次登录?

如果您需要使用Google凭据登录,则需要使用Google OAuth Id令牌或Google OAuth访问令牌.

要将Firebase OAuth登录状态从一个应用程序复制到另一个应用程序,您将从signInWithPopup结果中获取凭据,并在第二个实例中将其用于signInWithCredential.

two.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider()) .then(function(result) { return one.auth().signInWithCredential(result.credential); });