从Facebook登录获取信息

时间:2014-07-22 13:43:59

标签: javascript jquery html5 facebook

我想知道如何从我在网站上插入的Facebook登录按钮访问用户的姓名和电子邮件地址。我想保存用户的名称和相应分数,以便我可以更新它们。我尝试过很多方法,比如图形API,访问令牌,但没有任何对我有用......我正在使用Parse开发游戏。

  • 我想保存分数和用户名,包括云代码上的电子邮件(Parse)
  • 其次我想自动按降序打印记分板。

1 个答案:

答案 0 :(得分:0)

我认为你应该好好看看Developers Site of facebook

这是您想要的直接具体链接。

它为您提供了两种Facebook登录方法和获取用户详细信息(仅限那些不需要查看的方法)。 由于您有自己的按钮,因此您需要FB.login()方法登录并获取用户详细信息。文档清楚地解释了从加载,初始化,调用API等一步一步的流程。看看并告诉你哪里和哪个点让你感到困惑。

我假设您已经完成了SDK加载和初始化部分。让我们转到登录操作并获取用户数据。

  

这是你的按钮

<input type="button" value="Login Facebook" onclick="loginFacebook()" />

现在

<script>
/* Assuming Initialization and SDK loading done. Code is available there on the website */

 function LoginFacebook() {
    FB.login(function(response) { //This response here has the access token, signed request etc data.
       if (response.status === 'connected') {
             getUserInfo();
       } else if (response.status === 'not_authorized') {
               // Not Autorized to use this.
        } else {
              // The person is not logged into Facebook, so we're not sure if
              // they are logged into this app or not.
        }
        });


   function getUserInfo() {
      FB.api('/me?fields=id,name,email, function(response) {
           console.log(response); //Facebook returns a JSON object with this requested info
           console.log(response.name + "  " + response.email ); //or whatever you receive            
     });
   }
</script>
  

我还建议您使用Facebook图形浏览器等Facebook开发人员工具来检查API请求的响应等。我相信它会帮助你看到每个api请求的答案究竟是什么。