2

嗨,我已经在我的 react native 应用程序中实现了 Facebook 登录。sdk自带的标准按钮我没用过。一切正常登录成功。但现在我不知道如何从 Facebook 用户个人资料中提取数据,即姓名、电子邮件。

下面是我的代码

_fbAuth() {
        LoginManager.logInWithReadPermissions(['public_profile']).then(function (result) {
            if (result.isCancelled) {
                console.log('Login was Cancelled');
            } else {

                        const { navigate } = this.props.navigation;

                //
              console.log('Login Successful' + result.grantedPermissions.toString());



            }
        }, function (error) {
            console.log('An error has occured' + error);
        })
    }

按钮代码

<TouchableHighlight onPress={() => { this._fbAuth() }}>
                            <Image source={require('../images/facebook-logo.png')} style={styles.facebook}></Image>
                        </TouchableHighlight>
4

2 回答 2

4

登录成功时,您需要使用AccessToken函数和LoginManager来获取令牌。收到token后,我们可以使用这个token通过facebook的graph api来获取登录用户的数据。

考虑下面的代码:

import { LoginManager, AccessToken } from 'react-native-fbsdk'; 

async _fbAuth() {
    let { isCancelled } = await LoginManager.logInWithReadPermissions(['public_profile','user_posts']);

    if ( !isCancelled ) {
        let data = await AccessToken.getCurrentAccessToken();
        let token = data.accessToken.toString();
        await afterLoginComplete(token);
    }
    else {
        console.log('Login incomplete');
    }

}

const afterLoginComplete = async (token) => {
    const response = await fetch(
        `https://graph.facebook.com/me?fields=id,name,first_name,last_name,gender,picture,cover&access_token=${token}`);
    let result = await response.json();

    // use this result as per the requirement
};

你可以从这里学习如何使用 facebook graph API

于 2017-09-14T09:55:18.640 回答
1
import {AccessToken,LoginManager} from 'react-native-fbsdk';

  fbAuth() {
    var current =  this;
    const { navigate } = this.props.navigation;
      LoginManager.setLoginBehavior('web');
        LoginManager.logInWithReadPermissions(['public_profile','email']).then(
      function (result) {
        if (result.isCancelled) {
          console.log('Login was cancelled');
        } 


         else {
          //var accessToken  = null;

          AccessToken.getCurrentAccessToken().then(
            (data) => {
              console.log(data.accessToken.toString())
             const{accessToken}   = data

  // with the help of access token you can get details for fb login

            console.log('accessToken: ' + accessToken);
             fetch('https://graph.facebook.com/v2.5/me?fields=email,name,friends&access_token=' +accessToken)
             .then((response) => response.json())
             .then((json) => {
               console.log('jsonn: ' + json);
               console.log('Login name: ' + json.name);
               console.log('Login id: ' + json.id);
               console.log('Login email: ' + json.email);
               var nsn = json.name
               console.log('Login email state: ' +nsn);   
               var idfb = json.id
               console.log('Login email state: ' +idfb);  
               var idemail = json.email
               console.log('Login email state: ' +idemail);  

             })
             .catch(() => {
               reject('ERROR GETTING DATA FROM FACEBOOK')
             })

            })

        }
      },
      function (error) {
        console.log('Login failed with error: ' + error);
      }
    );
  }
于 2019-04-25T05:25:50.400 回答