我试图使用Angular2中的Firebase提供商访问Facebook的uid,但我无法从Facebook提供商处获得用户uid。使用Angular2获取提供商UID在Firebase中使用Angular2
当我使用authState.uid它显示我的firebase uid而不是Facebook的uid,文档已过时,authState.facebook.uid已弃用,所以我不知道正确的方式来获取Facebook用户ID 。
通过互联网,我看到它是一种使用类似authState.providerData.uid的方式获取Facebook用户标识的新方法,但在控制台中我得到了未定义。
这里是我的代码:
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);
});
}
感谢您的时间和帮助:)