I've made a little Christmas themed Facebook app which I want to add on a page as a tab but I am running into some issues which makes the app unusable.
The site URL is the following: https://mariusvaduva.com/bradbattle/
If I do the first login there I run into no issues but unfortunately when I test it directly on the Facebook Page Tab, as a new user, after the login I receive a blank page instead of the iframe and the following error:
Refused to display in a frame because it set 'X-Frame-Options' to 'DENY'.
This is how I get the login URL:
session_start();
require_once 'php/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'XXXXXX',
'app_secret' => 'XXXXXX',
'default_graph_version' => 'v2.8'
]);
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl('https://mariusvaduva.com/bradbattle/callback.php');
And this is how I display the link:
<div id="login_container">
<?php echo '<img id="login_btn" src="img/login.png">'; ?>
</div>
I've tried few suggestions including adding an .htaccess file with X-Frame-Options: ALLOW-FROM https://www.facebook.com/ but with no success.
What am I doing wrong? Is there another way of handling this? I really wanted to have this ready by this weekend. Any help or guidance is more than welcomed.
P.S. This is my callback file in case it helps:
session_start();
require_once 'php/Facebook/autoload.php';
require_once 'php/classes/class.database.php';
$fb = new Facebook\Facebook([
'app_id' => 'XXXXXX',
'app_secret' => 'XXXXXX',
'default_graph_version' => 'v2.8'
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (!isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
$oAuth2Client = $fb->getOAuth2Client();
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);
$tokenMetadata->validateAppId('1849207942031149');
$tokenMetadata->validateExpiration();
if (!$accessToken->isLongLived()) {
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string)$accessToken;
try {
$response = $fb->get('/me?fields=id,name', $_SESSION['fb_access_token']);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
$db = new Database();
$sql = 'SELECT id FROM users WHERE id = "%s" LIMIT 1';
$db->prepare($sql,array($user['id']));
$data = $db->execute();
if (empty($data)) {
$sql = 'INSERT INTO users (id, name) VALUES ("%s","%s")';
$db->prepare($sql,array($user['id'],$user['name']));
$data = $db->execute();
}
$_SESSION['user_id'] = (string)$user['id'];
$_SESSION['user_name'] = (string)$user['name'];
header('Location: https://mariusvaduva.com/bradbattle/bradbattle.php');
exit;
Related
I have created a basic log in for my app, login.php. It looks like this:
<?php
session_start();
require_once __DIR__ . '/Facebook/autoload.php';
$fb = new \Facebook\Facebook([
'app_id' => 'XXXXXXXXXXXX',
'app_secret' => 'YYYYYYYYYYYY',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['user_hometown']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://localhost:8080/myapp/fb-callback.php', $permissions);
echo 'Log in with Facebook!';
I'm now trying to add a logout link to fb-callback.php, to allow the user to log out from the app, but it's not working correctly - it redirects me to Facebook page instead of login.php on my localhost.
What am I doing wrong?
See the last lines of fb-callback.php for logout :
<?php
session_start();
require_once __DIR__ . '/Facebook/autoload.php';
$fb = new \Facebook\Facebook([
'app_id' => 'XXXXXXXXXXXX',
'app_secret' => 'YYYYYYYYYYYY',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=hometown', $accessToken);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Hometown: ' . $user->getHometown()->getField('name'). "<br>";
$logoutUrl = $helper->getLogoutUrl('{access-token}', 'http://localhost:8080/myapp/login.php');
echo 'Logout of Facebook!';
Also, is there a way to not display the user's access token in the URL after redirecting from login to fb-callback?
i get the following error from FaceBook api:
'Not Logged In: You are not logged in. Please login and try again.'
I'm using laravel 5, and php-sdk-v4 library so i add the callback page as fucnction in a controller, .
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Session;
use Facebook;
class FacebookLoginController extends MainController {
public function login() {
$fb = new Facebook\Facebook([
'app_id' => '****',
'app_secret' => '****',
'default_graph_version' => 'v2.7',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email'];
$loginUrl = $helper->getLoginUrl('http://localhost/tshop/public/fbcallback', $permissions);
//dd($fb);
echo 'Log in with Facebook!';
}
public function fbcallback() {
$fb = new Facebook\Facebook([
'app_id' => '1780077285568634',
'app_secret' => '625dc9eb7561f10bebc79ae2a1c96dca',
'default_graph_version' => 'v2.7',
]);
$helper = $fb->getRedirectLoginHelper();
$_SESSION['FBRLH_state']=$_GET['state'];
try {
$accessToken = $helper->getAccessToken();
} catch (Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (!isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
echo '<pre>';
print_r($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('1780077285568634'); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (!$accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
/* Get user details from facebook */
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name,email', $accessToken);
} catch (Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
echo '<hr>';
$user = $response->getGraphUser();
echo '<hr>';
echo $user['name'];
}
}
it is because OAuth redirect URI is either wrong or it can be if you have added www. in your url,if it is so remove www , simply "http://yoururl " .
Change this in your facebook app.
I made same mistake .Hope it will work for you.
I Add session_start() and it worked! thanks!
in my localhost site i am using login with facebook feature and it's working fine but now i want to store user details to my database so, i put some code in facebook SDK file fb-callback.php like...
<?php
session_start();
require_once 'facebook-php-sdk-v4/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'app_id', // Replace {app-id} with your app id
'app_secret' => 'app_secret',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
var_dump($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId("app_id"); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
$fbApp = new Facebook\FacebookApp('app_id', 'app_secret');
$request = new Facebook\FacebookRequest($fbApp, $accessToken, 'GET', '/me');
try {
$response = $fb->getClient()->sendRequest($request);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
include "config.php";
$query = "INSERT INTO users VALUES ('', '".$graphNode['name']."', '".$graphNode['email']."')";
$result = mysql_query($query);
if ($result) {
// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
header('location:index.php');
}
else{
echo "Problem In Login";
}
?>
but it inserting only name in database not email. Please help me.
it's done with this.
$_SESSION['fb_access_token'] = (string) $accessToken;
$fbApp = new Facebook\FacebookApp('1014758295283866', 'b1a98e587c8bef98dfb273db67214afb');
$request = new Facebook\FacebookRequest($fbApp, $accessToken, 'GET', '/me', ['fields' => 'id,name,email']);
try {
$response = $fb->getClient()->sendRequest($request);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
include "config.php";
$query = "INSERT INTO users VALUES ('', '".$graphNode['name']."', '".$graphNode['email']."')";
$result = mysql_query($query);
if ($result) {
// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
header('location:index.php');
}
else{
echo "Problem In Login";
}
I have trying to retrieve user profile in my website.
But after I get the access token, I can't use it. The result it show is
Graph returned an error: Invalid OAuth access token
I can get the access token by below code
Login.php
session_start();
require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'I have insert correct app id',
'app_secret' => 'and app secret',
'default_graph_version' => 'v2.0',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email'];
$loginUrl = $helper->getLoginUrl('http://myweb.com/login-callback.php', $permissions);
echo 'Log in with Facebook!';
?>
And below is login-callback.php
<?php
session_start();
require_once __DIR__ . '/facebook-php-sdk-v4-5.0-dev/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'I have insert correct app id',
'app_secret' => 'and app secret',
'default_graph_version' => 'v2.0',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
$response = $fb->get('/me?fields=id,name', "'".$_SESSION['fb_access_token']."'");
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
?>
Am I doing something wrong? Please help. Thanks.
I only have:
$response = $fb->get('/me?fields=id,name', $token);
$user = $response->getGraphUser();
It works perfectly
I think You should change your code :
"'".$_SESSION['fb_access_token']."'" to $accessToken
and try again
i have integrated facebook login (PHP Sdk Version 5.0.0) with my website. That is working fine in my localhost. But it shows the following error when i have hosted in my server;
Facebook SDK returned an error: Connection timed out after 10001
milliseconds
I have just changed the 'Site URL' and 'OAuth redirect URI' only when i hosted in server. Is it necessary to change anything else in my app? I have checked curl is enabled in my server, its showing 'enabled' in php ini. My PHP Version is 5.5.28.
My code is given below;
index.php:
<?php
session_start();
error_reporting(E_ALL);
$path = realpath(dirname(__FILE__));
require $path .'/facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxx',
'app_secret' => 'xxxxxxxx',
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getCanvasHelper();
// Grab the signed request entity
$sr = $helper->getSignedRequest();
// Get the user ID if signed request exists
$user = $sr ? $sr->getUserId() : null;
if ($user)
{
try
{
// Get the access token
$accessToken = $helper->getAccessToken();
}
catch (Facebook\Exceptions\FacebookSDKException $e)
{
// There was an error communicating with Graph
echo $e->getMessage();
exit;
}
}
else
{
$helper = $fb->getRedirectLoginHelper();
$permissions = ['public_profile','email','user_birthday','user_location']; // optional
$callback = 'http://www.example.com/ret_page.php';
$loginUrl = $helper->getLoginUrl($callback, $permissions);
echo 'Log in with Facebook!';
}
ret_page.php:
<?php
session_start();
error_reporting(E_ALL);
$path = realpath(dirname(__FILE__));
require $path .'/facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxx',
'app_secret' => 'xxxxxxxxxxx',
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getRedirectLoginHelper();
try
{
$accessToken = $helper->getAccessToken();
}
catch (Facebook\Exceptions\FacebookResponseException $e)
{
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}
catch (Facebook\Exceptions\FacebookSDKException $e)
{
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (!isset($accessToken))
{
if ($helper->getError())
{
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
}
else
{
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
$fb->setDefaultAccessToken($accessToken);
$res = $fb->get('/me?fields=id,name,gender,email,birthday,location');
$user = $res->getGraphObject();
// Printing result
print_r($user);