Can't seem to get a username displayed in Facebook App
Ok, so I am trying out my first Facebook app, a hello worldes开发者_StackOverflow社区que construction. Here's my code:
require_once 'facebook.php';
$appapikey = 'YOUR APPLICATION KEY';
$appsecret = 'YOUR APPLICATION SECRET YOUR';
$facebook = new Facebook($appapikey, $appsecret);
$user_id = $facebook->getUser();
echo "<p>Hello, <fb:name uid=\"$user_id\" />";
However, the username doesn't show! All it shows is:
Hello,
I would like a reliable method to pull the username!
You need to authenticate and connect with your app first. Here's a tutorial explaining the whole process:
http://thinkdiff.net/facebook/php-sdk-3-0-graph-api-base-facebook-connect-tutorial/
If you are not login with facebook connect , you can't get $user_id.
check example code from https://github.com/facebook/php-sdk/blob/master/examples/example.php
When you want to use the Facebook API, the user have to be of course logged in to the site, and he also has to be "connected" to it. That is, that the user must have authorized Facebook to give you some permissions over his account, like his info, publishing to his/her wall, etc.
That process is called "connecting" to Facebook and it must happen once, then there is no necessity as the user has already authorized the use of his information and/or the use of his fb account.
The connection happens in Facebook. It can be trough JavaScript. The user clicks a button and a Facebook window pops up asking the user to grant the requested permissions. If the user accepts, then your app can use Facebook. If he refuses, then you cannot use it. There is a callback function for each case. If you do it trough PHP, then you use a function in the PHP SDK to get a login URL. The user clicks on it and is temporarily redirected to fb. Then, he is shown an authorization box. He can accept or refuse. When he does, he'll be redirected back to your site.
精彩评论