I am new to FB graph API and I try to include the FB login in my website, I following the official FB login tutorial from their website, But after login I get the following error:
Failed to connect to graph.facebook.com port 443: Connection refused
Login.php
<?php
session_start();
require_once __DIR__ . '/fbsdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.3',
// . . .
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['public_profile', 'email']; // optional
$loginUrl = $helper->getLoginUrl('http://-myserver-/login-callback.php', $permissions);
echo 'Log in with Facebook!';
?>
login-callback.php
<?php
session_start();
require_once __DIR__ . '/fbsdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.3',
// . . .
]);
$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)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
}
?>
example-canvas-app.php
<?php
session_start();
require_once __DIR__ . '/fbsdk/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx',
'app_secret' => 'xxx',
'default_graph_version' => 'v2.3',
// . . .
]);
$helper = $fb->getCanvasHelper();
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)) {
// Logged in.
}
?>
I already searched in stack and google but I can't find any solution
Hostinger's free server does not allow a connection to Fb's SDK as mentioned by their customer service.
Related
I am trying from last day to post on user timeline and for this I am using facebook PHP SDK5. I have two files one is fb-integration.php and second is login-callback.php
In fb-intergration.php - I have following code which is basically to check user login or not.
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '2426534xxxxxxxx',
'app_secret' => 'dc36a92xxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'publish_actions']; // optional
$loginUrl = $helper->getLoginUrl('https://example.com/events/test/login-callback.php', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
?>
In login-callback file - I have following script:
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '2426534xxxxxxxx',
'app_secret' => 'dc36a92xxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getCanvasHelper();
try {
if (isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
} else {
$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 ($accessToken) {
echo 'Successfully logged in!';
}else{
echo 'Sorry not generated';
}
?>
Every time, I am getting result Sorry not generated.
Where am i doing wrong. why is not my accesToken generating?
I am trying to make a login page for v2.8 graph api.
I am following all the graph api examples but still getting an error.
This is the error:
Facebook SDK returned an error: Cross-site request forgery validation failed. Required param "state" missing from persistent data.
fbaccess.php
session_start();
$app_id = "xxxxxx";
$app_secret = "xxxxx";
$site_url = "http://xxxxxxxxx/";
require_once __DIR__ . '/src/autoload.php';
// Create our application instance
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.8',
]);
index.php
session_start();
include("fbaccess.php");
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl('http://shortbucks.com/facebook/callback.php', $permissions);
echo 'Log in with Facebook!';
callback.php
session_start();
include("fbaccess.php");
$helper = $fb->getRedirectLoginHelper();
try {
// Returns a `Facebook\FacebookResponse` object
$accessToken = $helper->getAccessToken();
$response = $fb->get('/me?fields=id,name', $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 'Name: ' . $user['name'];
Can someone help me identify the error?
Thanks
Hi I had the same problem, i have downloaded the last version of SDK from Facebook and voila works perfect.. cheers
I wrote a code for post a message on my page (not own timeline)
post.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxx',
'default_graph_version' => 'v2.5',
]);
$data = [
'message' => 'My awesome photo upload example.',
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/xxxxxxxxxxxxxx/feed', $data, $_SESSION['facebook_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;
}
$graphNode = $response->getGraphNode();
echo 'Photo ID: ' . $graphNode['id'];
index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$app_id = 'xxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxx';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'APP-ID|APP-SECRET'
]);
try {
$response = $fb->get('/xxxxxxxxxxxxxx?fields=id,name');
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
//exit; //redirect, or do whatever you want
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes', 'manage_pages', 'publish_actions'];
$loginUrl = $helper->getLoginUrl('http://xxxxxxxxxxxxxx/facebook/login-callback.php', $permissions);
echo 'Log in with Facebook!';
login-callback.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$app_id = 'xxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxx';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
'default_access_token' => 'APP-ID|APP-SECRET'
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
//echo 'Graph returned an error: ' . $e->getMessage();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
//echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
} elseif ($helper->getError()) {
// The user denied the request
}
header('Location: index.php');
It's just work when in file post.php use this code:
$response = $fb->post('/me/feed', $data, $_SESSION['facebook_access_token']);
And then post show on my timeline.
But when I want to post on page and use this:
$response = $fb->post('/xxxxxxxxxxxxxx/feed', $data, $_SESSION['facebook_access_token']);
don't work and not appears anythings, and don't show my post on the page or timeline!
I have a problem with a redirection after successful login in the file fb-callback.php. This line wont work: header("Location: http://website/project/main.php", true, 302);. I've marked before all var_dump's that the redirection should work but it wont work it will bring be back to index.php and in the browser bar is #_=_ after index.php. The cookies becomes set from facebook and the login page works too. What cause this?
index.php
session_start();
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.4',
'cookie' => true,
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_friends', 'public_profile']; // optional
$loginUrl = $helper->getLoginUrl("http://website/project/fb-callback.php", $permissions);
echo '<a class="ui-btn" href="' . $loginUrl . '" id="fb09">Log in with Facebook!</a>';
fb-callback.php
session_start();
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.4',
'cookie' => true,
]);
$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('');
// 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>";
exit;
}
//echo '<h3>Long-lived</h3>';
//var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
// User is logged in with a long-lived access token.
// You can redirect them to a members-only page.
header("Location: http://website/project/main.php", true, 302);
main.php
session_start();
require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.4',
'cookie' => true,
]);
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);