如何从 Firebase 管理员获取 Facebook ID?

Hun*_*ung 0 javascript node.js firebase firebase-authentication

我正在使用 Firebase Admin 登录我的 NodeJs 应用程序。客户端将通过发送 idToken(从 Firebase 获取)登录,然后 NodeJs 服务器将使用 admin.auth().verifyIdToken(idToken) 获取用户。

结果将如下所示:

{ iss: 'https://securetoken.google.com/myapp-5cde1',
  name: 'Manh Hung',
  picture: 'https://graph.facebook.com/185960225411xxx/picture',
  aud: 'xxxxxx-5cde1',
  auth_time: 1526626xxx,
  user_id: 'uCxxxxxxxxxxxxxxxQR4d2',
  sub: 'uCwNoxxxxxxxxxxxxxxuQR4d2',
  iat: 15266xxx1,
  exp: 15266xxx91,
  firebase:
   { identities: { 'facebook.com': [Array] },
     sign_in_provider: 'facebook.com' },
  uid: 'uCxxxxxxxxxxxxxxxQR4d2' 
}
Run Code Online (Sandbox Code Playgroud)

是的,我通过“sign_in_provider:'facebook.com'”获得了用户的注册方法。但我不知道如何获取用户的 Facebook ID。有人可以给我建议吗?非常感谢!

boj*_*eil 5

该特定信息在 ID 令牌中不可用。您需要使用 Firebase Admin SDK 来查找用户并检查 providerData:

admin.auth().getUser(uid)
  .then(function(userRecord) {
    // Assuming the first provider linked is facebook.
    // The facebook ID can be obtained as follows.
    console.log(userRecord.providerData[0].uid);
  })
  .catch(function(error) {
    console.log("Error fetching user data:", error);
  });
Run Code Online (Sandbox Code Playgroud)