I need your help.
I am using Kohana 3.0.4 framework for my web app and i want to use facebook login so i am using the Facebook Web SDK v5. I get every information from the the user profile except the user email. In my login action this what i did:
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxx',
'app_secret' => 'xxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_birthday', 'public_profile']; // optional
$callback = 'http://www.example.com';
$loginUrl = $helper->getLoginUrl($callback, $permissions); echo "<a href='.$loginUrl.'>Login With Facebook</a>";
then in the redirected action:
# login-callback.php
$fb = new Facebook\Facebook([
'app_id' => '1200538793346947',
'app_secret' => '3cf44820ffae5f16c8122174701a558f',
'default_graph_version' => 'v2.8',
]);
// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
$helper = $fb->getRedirectLoginHelper();
// $helper = $fb->getJavaScriptHelper();
// $helper = $fb->getCanvasHelper();
// $helper = $fb->getPageTabHelper();
try {
$accessToken = $helper->getAccessToken();
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/me?fields=email,name,birthday,gender', $accessToken);
} 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;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName(). ' ' . $me->getId() . ' '. $me->getGender() . ' '. $me->getEmail() . '<br />';
print_r($me->getBirthday());
Please i need help because i want to also get the user email. Thanks in advance.
Related
login.php codes
if(!session_id()) {
session_start();
}
require_once __DIR__ . '/vendor/autoload.php';
$facebook_appId = '123';
$facebook_appSecret = '123';
$fb = new Facebook\Facebook([
'app_id' => $facebook_appId,
'app_secret' => $facebook_appSecret,
'default_graph_version' => 'v2.11',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://www.123.com/login-callback.php', $permissions);
echo 'Login via Facebook!';
login-callback.php codes
if(!session_id()) {
session_start();
}
require_once __DIR__ . '/vendor/autoload.php';
$facebook_appId = '123';
$facebook_appSecret = '123';
$fb = new \Facebook\Facebook([
'app_id' => $facebook_appId,
'app_secret' => $facebook_appSecret,
'default_graph_version' => 'v2.11',
]);
// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
// $helper = $fb->getRedirectLoginHelper();
// $helper = $fb->getJavaScriptHelper();
// $helper = $fb->getCanvasHelper();
// $helper = $fb->getPageTabHelper();
try {
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
$response = $fb->get('/me?fields=id,name', $accessToken );
// $response = $fb->get('/me', $accessToken );
}
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;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
When I click on the "login.php" output link, it jumps to the "login-callback.php" and prints the following error message. How do I solve this problem?
Graph returned an error: Unable to load URL: The domain name of this URL does not contain the domain name of the application. In order to be able to load the URL, add all domains and subdomains of the application in the App Domain Name field of the app settings.
However, I have added the corresponding url in the facebook developer platform.
Now OAuth is mandatory in strict mode for FB Login:
You have to set the URI of your callback script in the params of FB Login in your app settings board, AND pass the same callback URL as an argument of getAccessToken() method:
try {
$accessToken = $helper->getAccessToken('http://yourdomain.com/login-callback.php');
}// ...
login.php codes
if(!session_id()) {
session_start();
}
require_once __DIR__ . '/vendor/autoload.php';
$facebook_appId = '123';
$facebook_appSecret = '123';
$fb = new Facebook\Facebook([
'app_id' => $facebook_appId,
'app_secret' => $facebook_appSecret,
'default_graph_version' => 'v2.11',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email']; // Optional permissions
$loginUrl = $helper->getLoginUrl('http://www.123.com/login-callback.php', $permissions);
echo 'Login via Facebook!';
login-callback.php codes
// error_reporting (E_ALL & ~E_NOTICE);
if(!session_id()) {
session_start();
}
require_once __DIR__ . '/vendor/autoload.php';
$facebook_appId = '123';
$facebook_appSecret = '123';
$fb = new \Facebook\Facebook([
'app_id' => $facebook_appId,
'app_secret' => $facebook_appSecret,
'default_graph_version' => 'v2.11',
]);
// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
// $helper = $fb->getRedirectLoginHelper();
// $helper = $fb->getJavaScriptHelper();
// $helper = $fb->getCanvasHelper();
// $helper = $fb->getPageTabHelper();
try {
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$helper = $fb->getRedirectLoginHelper();
$accessToken = $helper->getAccessToken();
$response = $fb->get('/me?fields=id,name', $accessToken );
// $response = $fb->get('/me', $accessToken );
}
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;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
When I click on the "login.php" output link, it jumps to the "login-callback.php" and prints the following error message. How do I solve this problem?
Graph returned an error: 无法加载网址: 这个 URL 的域名未包含应用的域名。为了能够加载 URL,请在应用设置的应用域名字段添加应用的所有域和子域。
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 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.