facebook-graph-api - 如何在 react-native-fbsdk 中使用图形 API?

标签 facebook-graph-api react-native

我在 github 上阅读了文档和 Facebook developers docs .
只有样本,仅此而已。没有 API 文档。

发出 Graph 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();
}

但是,我找不到任何进一步的文件。我不知道每个参数的作用。

上面这些代码的结果是这样的。
Success
我也不知道如何得到结果。

但是,当我尝试将 '\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
但它不打印出什么错误,只是对象 Object。

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

这是我的问题,我真的需要你帮助我:
首先,请检查javascript code我目前正在看的?
使用方法 图 API react-native-fbsdk 取回一些 用户信息 (例如:全名)并成功 显示它(使用警报)?
中的每个参数是什么GraphRequest() 做 ?
的结构是什么?错误 对象和 结果 对象在 _responseInfoCallback ?

解决方案
感谢@Samuel 的回答,我已经更新了我的代码

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
    } 
  }

*注意:对于console.log(),需要使用“远程调试JS”,然后打开Chrome开发者工具查看日志。

最佳答案

不幸的是,react-native-fbsdk 文档没有更新,示例无法正常工作。

我遇到了同样的问题,我通过尝试和错误解决了它。

要解决您的问题,您需要更改您的 图请求 添加 参数 字段 像这样:

  <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.")}/>

您需要启用 远程JS调试查看 console.log()信息。
https://facebook.github.io/react-native/docs/debugging.html

并且您可能需要获得一些权限才能获得比姓名和电子邮件更多的信息,因此最好查看 Facebook Graph API 文档:https://developers.facebook.com/docs/graph-api/overview/

引用:

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

关于facebook-graph-api - 如何在 react-native-fbsdk 中使用图形 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37383888/

相关文章:

facebook-graph-api - 是否可以从获取的 url 而不是规范的 url 中提取元数据?

facebook-graph-api - 我的应用程序的 Facebook 故事 : Unable to generate story

javascript - Facebook 桌面应用程序的重定向 url

Facebook 应用程序无法仅在 IE9 中显示

react-native - EXPO React Native - 无法初始化react-native-reanimated库

javascript - 按图像导航

react-native - 热重载在 native android 中不起作用

facebook-graph-api - 如何通过图形 API 或 FQL 获取历史 "Facebook Page Likes"数据

javascript - 错误: Unable to resolve module `react-redux` from `C:\Users\wanuuu27\AwesomeProject\App.js` : Module `react-redux` does not exist

reactjs - 在 ListView React Native 中循环嵌套数组对象