FB.api is returning undefined for response.name
I'm creating a facebook app in an iframe. The page is mostly working, but I want to use the currently logged in user's name in the page, for custom messaging, etc.
I'm loading the FB library like this:
<div id="fb-root"></div>
<script type="text/javascript" src="http://connect.facebook.net/en_US/all.js"></script>
<script type="text/java开发者_JAVA百科script" >
FB.init({
appId : '106832082707477',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
FB.getLoginStatus(function(response) {console.log("FB.getLoginStatus=" + response.status)});
</script>
I currently have the page in sandbox mode, but two of the 4 people receive FB.getLoginStatus=Connected, while the other two get FB.getLoginStatus=NotConnected.
For those who are not connected, when I call FB.api, it returns 'undefined' as the user name:
function testFBLoggedIn() {
//http://developers.facebook.com/docs/reference/api/user
FB.api('/me', function(response) {
alert(response.first_name);
}
}
I would really not like to have to have the user log into my application, since I'm not trying to access anything more than their first_name, last_name, and name user properties. Plus, this is an extra step for users to have to do and we've already sold the client on the current process.
Does anyone have any ideas as to how to consistently get a handle on the facebook user name barring throwing up an allow box?
Thanks.
If a user is not connected then you can't use /me
path, as it is referring to currently connected user.
You would be able to use /uid
path to get a name of any user, but the problem is if a user is not connected with your app then you don't know their uid.
So I don't think you can get user's name if they are not connected.
I am currently working on an app and when I make an API call to /me/friends, as a test user it returns the user id's, but not the names. When I use the same app from a real Facebook account it works normally.
I suspect this happens because all of the friends of the test user are also test users. So, if you have a similar problem try testing with a real Facebook user.
精彩评论