i am trying to write a Facebook application where once installed on a page provides a simple Form once this form is populated it would create a new table with this users username or userid.
So far i have the FBML form up and running using an external php file to process it all how can i get users username or userid to my forms php action file.
Are you using the PHP SDK or the javascript SDK? If you're using the PHP SDK, it's as simple as this:
require_once("facebook.php");
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
));
$user = $facebook->getUser(); // This is the FB user ID
$username;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
$username = $user_profile['username']; // this is the facebook username.
// If the user hasn't set it manually, it will be something
// like "john.doe.50"
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
Related
I have just started mingling with facebook's php sdk, but to no avail. Although php works fine for all the other stuff on my ubuntu instance, facebook's php sdk doesn't (i.e. php code is not executed). All dependencies (only curl and json) are enabled and working.
I tried to look into /var/log/apache2/error.log but it seems that the file is empty - but maybe I'm not looking in the correct place.
So my question is: what would be the steps for me to debug this problem! Thanks for helping a noob!
Here is the boilerplate index.php code for from facebook:
<?php
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
I have a problem with my logging into my Facebook application.
When I click "login" I get redirected to Facebook to accept the permissions then Facebook redirects me back. But it doesn't change anything. The URL down is dynamic generated. When I try the same script on an other URL that's not dynamic it's work.
Here is the URL there i try to perform a login.
http://www.testaiq.se/test_592.html
What could be the problem? PHP is behind the URL over here.
require_once("facebook/facebook.php");
// Creating our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX',
));
// Getting User ID
$user = $facebook->getUser();
// Get Access token
$access_token = $facebook->getAccessToken();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
// Retrieving user's friend list using fb graph api
$user_profile = $facebook->api('/me','GET');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
}
I am currently using the code below in my Codeigniter application to get Facebook data from an app user.
<?php
require_once('facebook-php-sdk/src/facebook.php');
// Create our Application instance (replace this with your appId and secret).
$facebook= new Facebook(array(
'appId' => '2453463636',
'secret' => '365653656565656',
'allowSignedRequest' => false,
));
$_REQUEST += $_GET;
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
// $user_profile = $facebook->api('/me');
$user_profile = $facebook->api('/me?fields=picture.width(100).height(100),first_name,last_name,username,email');
echo $user_profile['email'];
echo $user_profile['username'];
} catch (FacebookApiException $e) {
$user = null;
}
} else {
die('<script>top.location.href="'.$facebook->getLoginUrl().'";</script>');
}
?>
This works fine in connecting and returns the username of the user echoed on the page, however the email does not work. The email returns shows up as an error on the view "Undefined index: email".
I used this very same thing to get the email just a few days ago and it worked, however that was on localhost. It get the email fine on localhost, when pushed to the server, it doesn't recognize email, but it does recognize and get the username.
Why isn't the email being recognized?
I am using the latest version of the sdk: Facebook PHP SDK (v.3.2.3)
You can't do with this options. You need extended permission from Facebook. Check this link for more details:
http://developers.facebook.com/docs/authentication/permissions/
Facebook Doesn't share user email without their permission.
Every time I run this script it gives me a different access token. I thought the access token was unique to a user.. Also why is this script so unreliable, fails half the time.
My Facebook Access Token Page
Here is the code:
<?php
require 'lib/facebook.php';
require_once('C:\inetpub\storeboard.com\linkedin\extract_data.php');
$facebook = new Facebook(array(
'appId' => FACEBOOK_APPID,
'secret' => FACEBOOK_APP_SECRET
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
$access_token = $facebook->getAccessToken();
if (isset($access_token)) {
echo $access_token;
//header( 'Location: http://www.'.APP_ROOT_DOMAIN.'/facebook/save_token.asp?nToken='.$access_token);
}
?>
Have I cut out code that is required from the original: http://developers.facebook.com/blog/post/534/
or am I missing something.
require_once('C:\inetpub\storeboard.com\linkedin\extract_data.php');
This line of code just extracts the APPID and APPSECRET from the database.
I am a complete beginner when it comes to PHP, so any help would be greatly appreciated.
Many Thanks!
You don't need to play with the token to get simple authentication:
After you get facebook api instance:
$facebook = new Facebook(array(
'appId' => FACEBOOK_APPID,
'secret' => FACEBOOK_APP_SECRET
));
If you are using me API - get the user profile:
$user_profile = $facebook->api('/me');
then obtain email from it:
$email = $user_profile['email'];
At this point you check if the email is valid in your database - if yes start the user session and you are good to go.
Now I'm developing a mobile web application using facebook connect, but I am having problems with the OAuth Dialog. I use the following facebook documents for my reference:
http://developers.facebook.com/docs/guides/mobile/#web
I choose to use the OAuth Dialog instead of the Login Button because mobile browsers do not recognize the FBML (I use the Blackberry browser on testing). The OAuth Dialog also lets me add a list of permissions within the parameters of scope. But the problem is when I've logged in using the OAuth Dialog, the parameter $me wasn't recognized so that the login button still appears and doesn't not change to the logout button.
Here is an example of my code:
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$accessToken = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
error_log($e);
}
}
if ($me) {
echo "<img src=\"images/logoutFB.gif\">";
} else {
echo "<img src=\"images/loginFB.gif\">";
}
Is the parameter $me can't be used if I use the OAuth Dialog to connects to facebook?
So how do I know that I am already logged into facebook or not if I use the OAuth Dialog?
Please help if you guys have the solution.
Looks like you need to use the updated PHP-SDK
getSession() has been replaced by getUser():
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => '135669679827333',
'secret' => 'xxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}