简体   繁体   English

使用JavaScript SDK检索Facebook用户信息时出错

[英]Error retrieving Facebook user info using JavaScript SDK

I am trying to retrieve Facebook user info on a web app hosted on Google App Engine. 我正在尝试在Google App Engine托管的Web应用程序上检索Facebook用户信息。

The setup is: 1. web app hosted on the default appspot.com from Google. 设置为:1.由Google托管在默认appspot.com上的网络应用。 2. Have a Facebook app is registered, and configured for "Website with Facebook Login". 2.注册一个Facebook应用程序,并将其配置为“具有Facebook登录名的网站”。

I am using the FB JavaScript SDK to authenticate and retrieve data through the simple code below: 我正在使用FB JavaScript SDK通过以下简单代码进行身份验证和检索数据:

function login(){
    FB.getLoginStatus(function(r) {
         if(r.status === 'connected') {
                FB.api('/me', function(user) {
                      alert(JSON.stringify(user));
                  });
         }else{
            FB.login(function(response) {
                    if(response.authResponse) {
                        alert("Now.... Thats better with FB!");
                } else {
                    alert("You should connect to FB.");
                }
         });
     }
    });
};

But when I get the 'user' response back from the FB.api(...) call, its the following JSON: 但是,当我从FB.api(...)调用中获得“用户”响应时,它会显示以下JSON:

{"error":{"type":"http", "message":"unknown error"}}

In my Facebook app, my "Site URL" in "Website with Facebook Login" section of Facebook App Settings is pointing to a https address of my GAE app. 在我的Facebook应用程序中,“ Facebook应用程序设置”的“具有Facebook登录名的网站”部分中的“站点URL”指向我的GAE应用程序的https地址。 And I am not sure why such error shows. 而且我不确定为什么会显示这样的错误。

I spent alot of time searching through StackOverflow and entire web but no reference to this. 我花了很多时间在StackOverflow和整个网络中进行搜索,但没有对此进行参考。 Could someone please...really please, help! 有人可以请...真的请帮忙! :) :)

Thank you. 谢谢。

After quite some investigation and try/errors, I found what the issue is :) I was calling the login() function from an onclick event as follows: 经过大量调查和尝试/错误之后,我发现了问题所在:)我从onclick事件中调用了login()函数,如下所示:

<a href='' onclick='login();'>

Now the Facebook login dialog called in my login() was not returning properly due to a issue (feature?) which was unknown to me till this date. 现在,由于一个问题(功能?),我的login()中调用的Facebook登录对话框没有正确返回,直到现在为止我还不知道。 That I need to add return false; 我需要添加return false; to the end of function call in onclick to make sure the browser performs its default behaviour of closing the opened window (of which FB.login depends on, to store proper access key). 到函数末尾的调用onclick,以确保浏览器执行其默认行为,即关闭打开的窗口(FB.login依赖于此窗口,以存储适当的访问键)。 Therefore, since the window wasnt behaving properly, proper access key is not stored and found by FB.api, hence the call fails. 因此,由于窗口行为不正常,FB.api无法存储和找到正确的访问密钥,因此调用失败。 Long story short, the fix was as easy as this: 长话短说,修复很简单:

<a href='' onclick='login();return false;'>

Hope this helps others. 希望这对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM