【问题标题】:Get Provider UID in Firebase using Angular2使用 Angular2 在 Firebase 中获取提供者 UID
【发布时间】:2018-02-10 08:42:12
【问题描述】:

我正在尝试使用 Angular2 中的 firebase 提供程序访问 facebook uid,但我无法从 facebook 提供程序获取用户 uid。

当我使用 authState.uid 时,它向我显示了 firebase uid 而不是 facebook uid,文档已过时并且 authState.facebook.uid 已被弃用,因此我不知道获取 facebook 用户 ID 的正确方法。

通过互联网,我看到这是一种使用 authState.providerData.uid 之类的东西获取 facebook 用户 ID 的新方法,但在控制台中我无法定义。

这是我的代码:

export class AppComponent {
  user: Observable<firebase.User>;
  displayName;
  photoURL;


  constructor(public afAuth: AngularFireAuth, private db: AngularFireDatabase, private http: Http) {
    this.user = afAuth.authState;
  }

  ngOnInit() {
    this.afAuth.authState.subscribe(authState => {
      if (!authState) {
        console.log('NOT LOGGED IN');
        this.displayName = null;
        this.photoURL = null;
        return;
      }
      let userRef = this.db.object('/users/' + authState.uid)
      userRef.subscribe(user => {
        let url = `https://graph.facebook.com/v2.8/${authState.uid}?fields=first_name,last_name&access_token=${user.accessToken}`;
        this.http.get(url).subscribe(response => {
          let user = response.json();
          userRef.update({
            firstName: user.first_name,
            lastName: user.last_name
          });
        });
      });


        this.displayName = authState.displayName;
        this.photoURL = authState.photoURL;
        console.log(authState.uid);
    });
  }

感谢您的时间和帮助:)

【问题讨论】:

    标签: facebook angular firebase facebook-graph-api firebase-authentication


    【解决方案1】:

    好的,我这样解决了,我不知道这是否是最好的方法,但它对我有用......

    为了获取 Facebook uid 而不是使用的 authState.facebook.uid(我认为这已被弃用)我现在使用 authState.providerData[0].uid

    我已经尝试使用 authState.providerData.uid 但它不起作用,所以我将 [0] 放入数组并获取 uid。

    【讨论】:

      猜你喜欢
      • 2018-01-12
      • 2019-11-12
      • 2017-09-18
      • 2018-05-02
      • 2021-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多