【发布时间】: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