如何从react-native-fbsdk获取用户信息(电子邮件,姓名等)?

dan*_*ler 16 facebook react-native

当用户通过Facebook进行身份验证时,我正在尝试访问用户的电子邮件和名称以设置和帐户.我准备好了react-native-fbsdk的文档,但我没有在任何地方看到它.

hon*_*awd 47

接受的答案使用fetch但SDK也能够执行此请求...因此,如果有人想知道,这就是如何使用SDK完成的.

let req = new GraphRequest('/me', {
    httpMethod: 'GET',
    version: 'v2.5',
    parameters: {
        'fields': {
            'string' : 'email,name,friends'
        }
    }
}, (err, res) => {
    console.log(err, res);
});
Run Code Online (Sandbox Code Playgroud)

  • 这当然是首选方法,请参阅https://github.com/facebook/react-native-fbsdk#graph-requests了解更多详情 (3认同)

Nad*_*bit 30

你可以做这样的事情(这是我在我的应用程序中做的简化版本):

<LoginButton
  publishPermissions={['publish_actions']}
  readPermissions={['public_profile']}
  onLoginFinished={
    (error, result) => {
      if (error) {
        console.log('login has error: ', result.error)
      } else if (result.isCancelled) {
        console.log('login is cancelled.')
      } else {
        AccessToken.getCurrentAccessToken().then((data) => {
          const { accessToken } = data
          initUser(accessToken)
        })
      }
    }
  }
  onLogoutFinished={logout} />
Run Code Online (Sandbox Code Playgroud)

// initUser函数

initUser(token) {
  fetch('https://graph.facebook.com/v2.5/me?fields=email,name,friends&access_token=' + token)
  .then((response) => response.json())
  .then((json) => {
    // Some user object has been set up somewhere, build that user here
    user.name = json.name
    user.id = json.id
    user.user_friends = json.friends
    user.email = json.email
    user.username = json.name
    user.loading = false
    user.loggedIn = true
    user.avatar = setAvatar(json.id)      
  })
  .catch(() => {
    reject('ERROR GETTING DATA FROM FACEBOOK')
  })
}
Run Code Online (Sandbox Code Playgroud)

  • 出色的反应。我假设 SDK 会为这些请求内置一些东西,但常规的旧 http 请求就可以解决问题。谢谢! (2认同)

Nae*_*him 10

这对你有帮助,这对我有用

import React, { Component } from 'react';
import { View } from 'react-native';
import { LoginManager,LoginButton,AccessToken,GraphRequest,GraphRequestManager} from 'react-native-fbsdk';

export default class App extends Component<{}> {

  render() {
    return (
      <View style={styles.container}>

        <LoginButton
          publishPermissions={["publish_actions"]}
          onLoginFinished={
            (error, result) => {
              if (error) {
                alert("login has error: " + result.error);
              } else if (result.isCancelled) {
                alert("login is cancelled.");
              } else {
                AccessToken.getCurrentAccessToken().then(
                  (data) => {
                    const infoRequest = new GraphRequest(
                      '/me?fields=name,picture',
                      null,
                      this._responseInfoCallback
                    );
                    // Start the graph request.
                    new GraphRequestManager().addRequest(infoRequest).start();
                  }
                )
              }
            }
          }
          onLogoutFinished={() => alert("logout.")}/>
      </View>
    );
  }

  //Create response callback.
  _responseInfoCallback = (error, result) => {
    if (error) {
      alert('Error fetching data: ' + error.toString());
    } else {
      alert('Result Name: ' + result.name);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

详情请访问此处,它将为您提供更多帮助. Facebook登录反应