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?
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
// 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 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 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.
I have a current 3-page web site/app to pull/display data from Facebook using my PHP SDK code, I'm getting the error:
The redirect_uri URL must be absolute
This is because of this code below:
$params = array(
'scope' => 'ads_management',
'redirect_uri' => 'http://example.com/login-callback.php',
);
$loginUrl = $helper->getLoginUrl($params);
Before changing it to this, I didn't have this ^ at all, and just this line instead:
$loginUrl = $helper->getLoginUrl('http://example.com/login-callback.php');
If I were to query posts and stuff that didn't require permissions, things worked smoothly. But I need to do queries like the ones below, located in fb-stats.php. I guess the way I have the params is wrong, but I don't exactly know the right way to format it.
Here's fb.php:
<?php
session_start();
require_once '../vendor/autoload.php';
// Checks if token is set and redirects to another page.
if (isset($_SESSION['facebook_access_token'])) {
// Logged in!
header('Location: fb-stats.php'); exit();
}
$fb = new Facebook\Facebook([
'app_id' => 'MYAPPID',
'app_secret' => 'MYAPPSECRET',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
// The site below is 'example' is actually my live site.
$params = array(
'scope' => 'ads_management',
'redirect_uri' => 'http://example.com/login-callback.php',
);
$loginUrl = $helper->getLoginUrl($params);
echo 'Log in with Facebook!';
?>
Here's login-callback.php:
<?php
session_start();
require_once '../vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'MYAPPID',
'app_secret' => 'MYAPPSECRET',
'default_graph_version' => 'v2.5',
]);
$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 (! $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;
}
}
header('Location: fb-stats.php'); exit();
}
Here's fb-stats.php:
<?php
session_start();
require_once '../vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'MYAPPID',
'app_secret' => 'MYAPPSECRET',
'default_graph_version' => 'v2.5',
]);
$fb->setDefaultAccessToken('access-token');
try {
$response = $fb->get('/PAGEID/insights/page_impressions?period=day', '{access token}');
$insightsNode = $response->getGraphEdge();
}
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;
}
var_dump($insightsNode);
The query I'm trying to execute needs permissions of ads_management and read_insights but I'm confused as to how to get mutltiple scopes going aside from the error I'm currently getting for the redirect_uri not being absolute. How would I go about fixing these things?
$params = array(
'scope' => 'ads_management',
'redirect_uri' => 'http://example.com/login-callback.php',
);
$loginUrl = $helper->getLoginUrl($params);
The getLoginUrl method expects two parameters - but you are passing only one array to it here.
The correct way to use this method is
$helper->getLoginUrl('http://example.com/login-callback.php', ['ads_management']);
The redirect URI is passed as the first parameter (string value), and then the second parameter is the scope (array of permissions you want to ask for.)
(Whether you want to put those values into a different array for configuration purposes beforehand, is up to you.)
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.