首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在react-native-fbsdk中使用图形API?

如何在react-native-fbsdk中使用图形API?
EN

Stack Overflow用户
提问于 2016-05-23 14:24:58
回答 5查看 15.2K关注 0票数 24

我在githubFacebook developers docs上阅读了文档。

只有样本,没有更多。无API文档。

发出Graph API请求的代码是

代码语言:javascript
复制
const infoRequest = new GraphRequest(
  '/me',
  null,
  this._responseInfoCallback,
);

和回调

代码语言:javascript
复制
_responseInfoCallback(error: ?Object, result: ?Object) {
  if (error) {
    alert('Error fetching data: ' + error.toString());
  } else {
    alert('Success fetching data: ' + result.toString());
  }
}

下面是发出Graph API请求的函数

代码语言:javascript
复制
testRequestGraphAPI(){
  const infoRequest = new GraphRequest(
  '/me',
  null,
  this._responseInfoCallback,
  );   
    new GraphRequestManager().addRequest(infoRequest).start();
}

但是,我找不到任何进一步的文档。我不知道每个参数是做什么的。

上述代码的结果如下所示。

我也不知道如何得到结果。

但是,当我尝试将'\me‘修改为'me?fields=id,name’时,它失败了。尽管我已经请求允许

代码语言:javascript
复制
<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.")}/>  

但它并没有打印出什么错误,只是对象对象。

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

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

首先,请检查一下我当前正在查看的javascript code

如何在react-native-fbsdk中使用图形接口来检索一些用户信息警报(示例:)并成功显示 it (使用警报)?

GraphRequest()中的每个参数都有什么作用?

_responseInfoCallback中的error对象和result对象的结构是什么?

解决方案

感谢@Samuel answer,我已经更新了我的代码

代码语言:javascript
复制
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();
  }

和回调

代码语言:javascript
复制
  _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开发者工具查看日志。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-05-24 08:01:54

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

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

要解决您的问题,您需要更改您的GraphRequest,将参数字段添加到其中,如下所示:

代码语言:javascript
复制
  <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调试才能查看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

票数 41
EN

Stack Overflow用户

发布于 2017-05-04 21:15:34

下面是一个自定义按钮的示例,如果您想创建一个按钮的话:)

代码语言:javascript
复制
FbLoginButton() {
 LoginManager
  .logInWithReadPermissions(['public_profile'])
  .then(function (result) {
    if (result.isCancelled) {
      alert('Login 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()

        })
    }
  }, function (error) {
    alert('Login fail with error: ' + error);
   });
  }
票数 6
EN

Stack Overflow用户

发布于 2018-08-30 23:35:34

谢谢你@Samuel。

多亏了你的帮助,我终于成功地从Facebook登录中获得了用户信息!

但我努力弄清楚如何从结果对象中获取用户名和电子邮件,因为我是React & Javascript的新手。

P.S.结果“name”是点,因为它是object!!

所以我在你的代码中添加了一些代码,供其他像我一样的人使用。

如果你不喜欢使用你的代码,那就告诉我。

代码语言:javascript
复制
<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)

                // Here's my code
                alert('Success fetching data: ' + result["name"].toString() + 
                ", " + result["email"].toString()); 
                /*  
                if(your DB already got this email or something unique) {
                  // SignIn()
                } 
                // when your DB doesn't have this email
                else {
                  // Do signUp() with this infomation and SignIn()
                }
                */
              }
            }

            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.")}/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37383888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档