本文介绍了firebase Facebook oAuth addScope()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 firebase with angular 和 oAuth 与 firebase facebook provider.一切正常.但是,当我尝试为示例添加新范围 (user_friends) 时,我没有得到任何数据(弹出窗口要求我提供正确的范围):

I am using firebase with angular and oAuth with the firebase facebook provider. Everything is working fine.However, when I am trying to add a new scope (user_friends) for examples I don't get any data (the popup asks me for the correct scopes):

//auth.service.ts
 signInWithFacebook() {
    const provider =  new firebase.auth.FacebookAuthProvider();
    provider.addScope("user_friends");
    return this.oAuthLogin(provider);
 }
 private oAuthLogin(provider) {
    return this._firebaseAuth.auth.signInWithPopup(provider)
    .then((credential) => {
       this.updateUserData(credential.user)
    })
 }

所以凭证像往常一样获得了基本的用户信息(displayName,photoUrl).我试图寻找返回的 Promise 作为回调:

So the credential got basic userInformation (displayName, photoUrl) as usual.I tried to look for the returned Promise as a callback :

type UserCredential = {
   additionalUserInfo?: firebase.auth.AdditionalUserInfo | null;
   credential: firebase.auth.AuthCredential | null;
   operationType?: string | null;
   user: firebase.User | null;
};

我们可以看到 firebase.user 获得了很多方法,但没有为我的新范围添加字符串.

We can see that there is a firebase.user getting alot of method but no adding string for my new scope.

interface UserInfo {
    displayName: string | null;
    email: string | null;
    phoneNumber: string | null;
    photoURL: string | null;
    providerId: string;
    uid: string;
}

因此我看不到如何将我的 user_friends 数据(一个数组(如下))添加到凭据数据(哪种方法?)并显示在我的控制台中.

Thus I dont see how my user_friends data, which is an array (below) can be added to the credentials data (which method?) and displayed in my console.

"friends": {
  "data": [
    //friends id and their name
  ],

  "summary": {
    "total_count": 631
  }
}

感谢您的帮助!

推荐答案

其实我已经找到了解决方案,想分享给像我这样迷路的人.

I actually found a solution, and wanted to share for the lost souls like me.

1) addScope() 方法只是在 oAuth 弹出窗口中询问额外范围的方法,回调中没有提供数据.

1) The addScope() method is just a method for asking additional scopes in the oAuth popup, no data provided in the callback.

2) 需要像 API 一样通过 graphUrl 使用 AccessToken 来获取 Facebook 附加范围

2) Getting Facebook additional scopes needs to be done with the AccessToken through the graphUrl like an API

3) 这里是获取可行 accessToken 的代码:

3) Here the code to get a viable accessToken :

private oAuthLogin(provider) {
return this._firebaseAuth.auth.signInWithPopup(provider)
  .then((result) => {
      this.token = result.credential["accessToken"];
    this.updateUserData(result.user)
  })
}

firebase/js 文档通过 result.credential.accessToken 获取 AccessToken,但在很多情况下会生成 typescript 错误(很多人收到此错误消息).

The firebase/js documentation get the AccessToken through result.credential.accessToken, but it generates a typescript error in many cases (alot of people got this error message).

因此,例如获取 Facebook 好友列表(使用 GraphUrl):

Thus, Getting Facebook friends list (with GraphUrl) for example :

  getFacebookFriendsList() {
var graphUrl = 'https://graph.facebook.com/me/friends?access_token=' + this.token;
return this.http.get(graphUrl);
}

这篇关于firebase Facebook oAuth addScope()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:05