我在githubFacebook developers docs上读了文件

发出图形API请求的代码是

const infoRequest = new GraphRequest(
  '/me',
  null,
  this._responseInfoCallback,
);

以及回调

_responseInfoCallback(error: ?Object, result: ?Object) {
  if (error) {
    alert('Error fetching data: ' + error.toString());
  } else {
    alert('Success fetching data: ' + result.toString());
  }
}

下面是生成图形API请求的函数

testRequestGraphAPI(){
  const infoRequest = new GraphRequest(
  '/me',
  null,
  this._responseInfoCallback,
  );   
    new GraphRequestManager().addRequest(infoRequest).start();
}

然而,我找不到任何进一步的文件.我不知道每个参数的作用

The result for these codes above is this.
Success
I also don't know how to get the result.

但是,当我试图将"\me"修改为"me"时?fields=id,name',失败.

<LoginButton
  publishPermissions={["publish_actions,user_birthday, user_religion_politics, user_relationships, user_relationship_details, user_hometown, user_location, user_likes, user_education_history, user_work_history, user_website, user_managed_groups, user_events, user_photos, user_videos, user_friends, user_about_me, user_status, user_games_activity, user_tagged_places, user_posts, user_actions.video, user_actions.news, user_actions.books, user_actions.music, user_actions.fitness, public_profile, basic_info"]}
  onLoginFinished={
    (error, result) => {
      if (error) {
        alert("login has error: " + result.error);
      } else if (result.isCancelled) {
        alert("login is cancelled.");
      } else {
        AccessToken.getCurrentAccessToken().then(
          (data) => {
            meow_accesstoken = data.accessToken
            alert(meow_accesstoken.toString())
          }
        )
      }
    }
  }
  onLogoutFinished={() => alert("logout.")}/>  

Failed

所以,问题是我不理解Facebook提供的没有解释的示例代码

这里是我的问题,我真的需要你帮助我:

SOLUTION

testRequestGraphAPI: function(){    
        const infoRequest = new GraphRequest(
          '/me',
          {
            parameters: {
              fields: {
                string: 'email,name,first_name,middle_name,last_name' // what you want to get
              },
              access_token: {
                string: meow_accesstoken.toString() // put your accessToken here
              }
            }
          },
          this._responseInfoCallback // make sure you define _responseInfoCallback in same class
        );
    new GraphRequestManager().addRequest(infoRequest).start();
  }

那回电话呢

  _responseInfoCallback: function(error: ?Object, result: ?Object) {
    alert("meow response");
    if (error) {
      alert('Error fetching data: ' + error.toString());
      console.log(Object.keys(error));// print all enumerable 
      console.log(error.errorMessage); // print error message
      // error.toString() will not work correctly in this case
      // so let use JSON.stringify()
      meow_json = JSON.stringify(error); // error object => json 
      console.log(meow_json); // print JSON 
    } else {
      alert('Success fetching data: ' + result.toString());
      console.log(Object.keys(result)); 
      meow_json = JSON.stringify(result); // result => JSON
      console.log(meow_json); // print JSON
    } 
  }

*注:用于控制台.log(),您需要使用"远程调试JS",然后打开Chrome developer tools查看日志(log).

推荐答案

不幸的是,react native fbsdk文档没有更新,示例也不能很好地工作.

我也遇到了同样的问题,我通过反复try 解决了它.

要解决您的问题,您需要将GraphRequest改为paramsfields,如下所示:

  <LoginButton
    onLoginFinished={
      (error, result) => {
        if (error) {
          alert("login has error: " + result.error);
        } else if (result.isCancelled) {
          alert("login is cancelled.");
        } else {

          AccessToken.getCurrentAccessToken().then(
            (data) => {
              let accessToken = data.accessToken
              alert(accessToken.toString())

              const responseInfoCallback = (error, result) => {
                if (error) {
                  console.log(error)
                  alert('Error fetching data: ' + error.toString());
                } else {
                  console.log(result)
                  alert('Success fetching data: ' + result.toString());
                }
              }

              const infoRequest = new GraphRequest(
                '/me',
                {
                  accessToken: accessToken,
                  parameters: {
                    fields: {
                      string: 'email,name,first_name,middle_name,last_name'
                    }
                  }
                },
                responseInfoCallback
              );

              // Start the graph request.
              new GraphRequestManager().addRequest(infoRequest).start()

            }
          )

        }
      }
    }
    onLogoutFinished={() => alert("logout.")}/>

您需要启用Remote JS Debug以查看console.log()信息.

可能你需要获得一些权限才能获得比姓名和邮箱更多的信息,所以最好查看Facebook Graph API文档:https://developers.facebook.com/docs/graph-api/overview/

参考:

https://github.com/facebook/react-native-fbsdk/issues/105#issuecomment-206501550

React-native相关问答推荐

ITMS—91053:缺少API声明—ReactNative

React Native:如何以编程方式对图像进行黑白过滤?

useState 函数似乎阻止了 Animated.timing 事件?

react-redux connect 和 redux 数据的组件生命周期顺序

React Native Android:how to remove "overscroll effect" of ScrollView

如何在 react-native 中设置 Onpress On Textinput

可能的未处理promise 拒绝(id:0):错误: "getLoginData" is read-only

react-native redux 和 ListView

更改整个 React Native App 的语言

如何将 View 定位在 ScrollView 的底部?

在 React Native 中无法滚动到 ScrollView 的底部

React Native Negative shadowOffset 创建顶部阴影

在 React Native 视图上强制 onLayout

在 React Native 中,如何使用浅渲染测试我的组件?

在 `../node_modules/react-native/React` 中找不到 `React-Core` 的 podspec

Node.js 在 React-Native 中的作用是什么?

如何使用 Jest 测试导入自定义原生模块的 React Native 组件?

React Native <>(空)组件到底是什么

React Native:无法解析模块 fs

React Navigation获取堆栈标题高度