oauth with facebook - php

I am trying to get if user is logined in facebook, if yes then I need to store their profile pic url, name in my db, so that users can comment in my web application.
I cant do this, I created a facebook application for website and using the following code for auth purpose. But its showing "An error occurred. Please try again later."
Any one please help me ? or help me with working code ?
<?php
require_once('../src/facebook.php');
$fb_app_id = "key";
$fb_secret = "secret";
$fb_app_url = "url";
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
// The user is logged in
try {
$user_profile = $facebook->api('/me');
// Here : API call succeeded, you have a valid access token
} catch (FacebookApiException $e) {
// Here : API call failed, you don't have a valid access token
// you have to send him to $facebook->getLoginUrl()
$user = null;
}
} // else : the user is not logged in
?>
<?php if ($user): ?>
Logout of Facebook
<?php else: ?>
Login with Facebook
<?php endif ?>

You can try this
require_once('../src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxx',
'cookie' => true,
));
$fb_user_id = $facebook->getUser();
try
{
$fb_user_profile = $facebook->api('/me');
$me = $facebook->api('/me');
}
catch (FacebookApiException $e)
{
$fb_user_id = NULL;
}
$fbid = $me['id'];
$firstname = $me["first_name"];
$lastname = $me["last_name"];
$gender = $me["gender"];
$email = $me["email"];
if ($facebook->getSession()) {
echo '<div id="fbc-img"><img src="http://graph.facebook.com/'.$me['id'].'/picture?type=normal" width="56" height="56"></div>
<div id="fbc-info">Welkom, '.$firstname.' ! <br>Logout </div> ';
} else {
echo 'Login met facebook ';
}

Related

PHP Facebook SDK doesn't work and returns code

My PHP Facebook SDK is not working and always returns $user=0 Why?
It is my first time to deal with facebook sdk
my code:
require './facebook-php/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxx'
));
// Get User ID
$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) {
error_log($e);
$user = null;
}
}
if($user) {
$logoutUrl = $facebook->getLogoutUrl(); echo 'there is a user';
} else {
$loginUrl = $facebook->getLoginUrl();
echo 'there is noo user <a href='.$loginUrl.'>login</a>';
}
below is the url return code :
http://localhost/e-commerce/testit.php?code=AQA8DckDMqNarptbCUIfAmn2tIyn1mjVBmHTjOMaQ4GqsFW_6vqemGW35TC07Kxq7kuB4QkKibHJ2-m9GntIx0biWEiR_FUuozsONvgc_tNmtGURpcm78DnIE26vT6hbjz10eZECuh-GLec9tGplhLrDNbLO-aX2Hv2Mu-8CQhAdiFQ6AYW3Nt09a8Rzahbn35Jz0cNyB_G7Ksjorq3iSs6Bpa6Tk0d7Vx0A0f1Mg77PgD6IuN_grjkNHDWmb_ywoqW48IqoxJUhnRmL3qi1G-CT7ft_f77rRr-VP-yQ-LTkdNja0rBzoFnFGccMuQ86u8tk7nfnx5qrSLBpHLEIBqQT&state=84ff9f36e8de53d13c96bcf7cb2da3b3#=
I have uploaded Facebook setting image
Facebook settings images

How do i get facebook usernames?

Im a totally facebook newbie developer.This is the sdk version that i use: click . This is the login part:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXx',
'cookie' => true
));
?>
<?php
$loginUrl = $facebook->getLoginUrl(array (
'scope' => 'email',
'redirect_uri' => 'http://facebook.ebis-servicii.ro/mytest/test.php'
));
$user = $facebook->getUser();
echo 'Login Here ';
?>
And this is the part where I want to get some informations about the user:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXX',
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try{
$user_profile = $facebook->api('/me');
var_dump($user_profile);
$fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $user_profile['username']; // To Get Facebook Username
$fbfullname = $user_profile['name']; // To Get Facebook full name
$femail = $user_profile['email']; // To Get Facebook email ID
}catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
echo "FBID ".$fbid."<br>";
echo "Email ".$femail."<br>";
echo "Name ".$fbfullname."<br>";
echo "Username ".$fbuname."<br>";
echo "Facebook Picture:<br>";
//echo '<img src="https://graph.facebook.com/'.$_SESSION["USERNAME"].'>;
echo "<img src ='https://graph.facebook.com/".$fbuname."'>";
}
?>
This is the url link where you see the results. So my question is how do I get the username? In the result of the var dump there is no username .
Since v2.0, the username is not included in the result anymore, as you can see in the Facebook docs: https://developers.facebook.com/docs/graph-api/reference/v2.1/user
Else, the App Scoped IDs would be pointless, and they came with v2.0 too. Apps should not be able to get to the "real" profile of the users.
You should be able to use the App Scoped ID to show the user picture:
echo '<img src="https://graph.facebook.com/'.$fbid.'/picture?width=121&height=100" />';
Btw, i would suggest using the new PHP SDK (4.x) for new Apps.

How to get user profile info using access token in php-sdk

I want to get user profile using access token that i have already generated, and if the access token is invalid it shoud display error. anyone can help to do this ? i want php code to handle this. i am waiting....
Since you already have the access token, the simplest and clean way would be-
$user_details = "https://graph.facebook.com/me?access_token=" .$access_token;
$response = file_get_contents($user_details);
$response = json_decode($response);
print_r($response);
First check if the user is logged in
$facebook = new Facebook(array(
'appId' => 'AppIDHere',
'secret' => 'AppSecretHere',
));
// See if there is a user from a cookie
$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;
}
}
If your access token is not null, fetch user data
<?php if ($user): ?>
Profile Picture: <img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
User Id: <?php echo $user_profile['id'];?>
Name: <?php echo $user_profile['name'];?>
First Name: <?php echo $user_profile['first_name'];?>
Last Name: <?php echo $user_profile['last_name'];?>
Link: <?php echo $user_profile['link'];?>
Gender: <?php echo $user_profile['gender'];?>
Username: <?php echo $user_profile['username'];?>
<?php else: ?>
<strong>You are not Logged In.</strong>
<?php endif ?>
The user variables can be got from Facebook Graph API
If You want to get user_email and other info then you can use this code....
$facebook = new Facebook(array(
'appId' => 'AppIDHere',
'secret' => 'AppSecretHere',
));
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me?fields=email,name,id,gender');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
I used facebook graph Api
$facebook = new Facebook(array('appId' => $fbconfig['appid'],'secret' =>
$fbconfig['secret'],'cookie' => true,));
$session = $facebook->getUser();
if($session)
{
try{
$uid = $session;
$fbme = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
// FQL : to fetch the email id
$fql = "SELECT email FROM user where uid='".$uid."'";
$response = $facebook->api(array('method' =>
'fql.query','query' =>$fql,));
$user_email = $response[0][email];
// Friend COunt.
$friend_res = $facebook->api('/me/friends');
$friend_count = count($friend_res[data]);
//likes
$likes = $facebook->api('/me/likes');
$fb_like = $likes[data];
// Getting more user personal data
$param = array(
'method' => 'users.getinfo',
'uids' => $uid,
'fields' => 'name,sex,pic,current_location,profile_url,email,birthday',
'callback'=> ''
);
$userInfo = $facebook->api($param);
}catch(FacebookApiException $e){
}
}

Facebook OAuth Dialog, white screen

I am trying to get an access token using the latest Facebook SDK for PHP. To my knowledge I am doing everything correct, but when it redirects to facebook.com/oauth/dialog, it just has a white screen. My code:
<div class="authorize_btn" style="float:right; margin-top:-35px; padding-right:10px">
<?php
include ('facebook/facebook.php');
$facebook = new Facebook(array( 'appId' => 'xxxxx',
'secret' => 'xxxx',
'cookie' => true ));
$session = $facebook->getUser();
$me = null;
if ($session) {
try {
$me = $facebook->api('/me');
}
catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
if ($me) {
$logout = $facebook->getLogoutUrl();
echo "<button class='btn btn-primary'></button";
} else {
$login = $facebook->getLoginUrl();
echo "<a href='$login'>Authorize</a>";
}
?>
</div>
Get the latest Facebook SDK for PHP - the one you are using is about 2 years old and not supported .
https://developers.facebook.com/docs/php/gettingstarted/4.0.0

$user = $facebook->getUser() returns 0 with SDK 3.2.2

I m using the facebook sample javascript code that come with sdk but no luck
i am using the latest 3.2.2 SDK
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '---------',
'secret' => '----------',
));
// See if there is a user from a cookie
echo $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;
}
}
Use this code:
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '---------',
'secret' => '----------',
));
$querystr=http_build_query($_REQUEST);
// See if there is a user from a cookie
$user = $facebook->getUser();
if(isset($_REQUEST['code'])) {
if(!empty($_REQUEST['code'])) {
setcookie("access_token", $_REQUEST['code'], time()+120);
}
echo ("<script> top.location.href='<YOUR APP URL>'</script>");
exit;
}
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;
}
}else {
$scope='email';
$params = array(
'redirect_uri' => 'YOUR SITE URL(WHere app is hosted)',
'display' => 'page',
'scope' => $scope
);
$loginurl = $facebook->getLoginUrl($params);
echo ("<script> top.location.href='".$loginurl."'</script>");
exit;
}

Categories