Hi Sir Am Using Fb Php Sdk 4 & Graph 2.8 I Know Its Little Outdated But I Want Continue On It. Only Authorized Problem Fixed Can Make My Project Done
I Used Configured & Login Data Page Below But Its Return Me
Graph returned an error: This authorization code has been used.
below my code
config.php
<?php
session_start();
$_IM=array(
"sitename" => "AppsFunny",
"siteurl" => "http://appsfunny.com",
"sitelogo" => "AppsFunny.Com",
"fb_page" => "/appsfunny",
"aurl" => "appsfunny.com"
);
include "ifunc.php";
include "db.php";
include "Unicode2Bijoy.class.php";
$app_id='423436984657946';
require_once __DIR__ . '/vendor/autoload.php';
use Facebook\FacebookRequest;
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => 'c82ff16cec2f6a055302c5c632c0129b',
'default_graph_version' => 'v2.8',
]);
if (isset($_SESSION['access_token'])) {
$token = $session->getToken();
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me', $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();
$_SESSION['FBID'] = $user['id'];
$_SESSION['FULLNAME'] = $user['name'];
$_SESSION['propic']="https://graph.facebook.com/".$_SESSION['FBID']."/picture?height=200&width=200";
}
?>
and login.php
<?php
require_once __DIR__ . '/config.php';
$p=$_GET["p"];
$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)) {
$access_token = $session->getToken();
$_SESSION['access_token'] = (string) $accessToken;
header("location:".$p);
}
Cause
This problem is there because you are trying to request access_token on every page load or reload. When you request a token, a unique code is get passed along with the URL. When you request again and again with the same token, this error occurs.
Solution
So after getting access_token, keep it in db. And request for new token only and only if either you don't have access_token or its not valid anymore.
Related
I m trying to get User info from facebook login api of php sdk. here is my config script.
I tried to used this fb guidelines but still i m not getting it properly.
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', '{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'];
I m unable to get the data of user. Please help me where I am making mistake.
<?php
if(!session_id()){
session_start();
}
// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-sdk/src/Facebook/autoload.php';
// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
/*
* Configuration and setup Facebook SDK
*/
$appId = '1698023223833500'; //Facebook App ID
$appSecret = 'e4113b39035af477669c40c9c87bc17b'; //Facebook App Secret
$redirectURL = 'http://localhost/'; //Callback URL
$fbPermissions = array('email'); //Optional permissions
$fb = new Facebook(array(
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.2',
));
// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();
// Try to get access token
try {
if(isset($_SESSION['facebook_access_token'])){
$accessToken = $_SESSION['facebook_access_token'];
}else{
$accessToken = $helper->getAccessToken();
echo $accessToken;
}
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
?>
Try below code
$me = $response->getGraphUser();
$fullname = $me->getProperty('name');
Disclaimer: I am relatively new to php coding and very new to the Facebook SDK.
I have been trying to learn the Facebook API with their online materials and examples and find them to be rather lacking. I am trying to do something very basic - simply login with Facebook and retrieve a few user values.
Following Facebook's example I have login.php which looks like this:
<?php
session_start();
require_once __DIR__ . '/../_fb/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '26777overwritten335',
'app_secret' => '8260906deoverwrittenf286510',
'default_graph_version' => 'v2.6',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl('http://www.domain.com/_mdp/fb_dev/login-callback.php', $permissions);
echo 'Log in with Facebook!';
?>
...and that seems to work fine.
On the login-callback.php page I have the following:
<?php
session_start();
require_once __DIR__ . '/../_fb/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '267772overwritten335',
'app_secret' => '8260906de2overwritten1f286510',
'default_graph_version' => 'v2.6',
]);
$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;
echo 'You are now logged in!';
echo '<br> the access token is: ' . $accessToken . '<br>';
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
}
// Am Successfully Logged In Here
try {
// Returns a `Facebook\FacebookResponse` object
$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'];
// OR
// echo 'Name: ' . $user->getName();
?>
I do appear to be able to successfully login as I get the echo from the line:
echo 'You are now logged in!';
I have tried so many different examples of code and get various errors but the above code results in being echoed back the following:
Graph returned an error: Invalid OAuth access token.
And I have googled that every way I can think of and have yet to find out what I am doing wrong.
If anyone can show me where I am missing something and hopefully give me some corrected codelines it would certainly be appreciated.
Also, FYI - I really want to use graph version 2.6 with SDK 5 because it appears there are a few edges available that aren't in recent versions.
Thanks so much for any help pointing me in the right direction.
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', '$accessToken');
You are passing the literal value $accessToken here - and that is of course not a valid access token.
PHP does not parse variables in strings with single quotes. Please go read http://php.net/manual/en/language.types.string.php in that regard, this is basic knowledge you need.
And then, stop putting simple variables into strings altogether.
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', $accessToken);
Here's my code to retrieve user's info from Facebook. I am using PHP SDK.
public function indexAction()
{
$fb = new Facebook\Facebook([
'app_id' => '934741473308351',
'app_secret' => 'de001d018a7769eb17eb11300e772a0c',
'default_graph_version' => 'v2.5',
'default_access_token' => isset($_SESSION['facebook_access_token']) ? $_SESSION['facebook_access_token'] : 'APP-ID|APP-SECRET'
]);
try {
if(isset($_SESSION['facebook_access_token'])){
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
Mage::log("Email====>Before".$userNode->getFirstName());
// echo 'Name: ' . $user['first_name'];
$this->_redirectUrl('/app-new/app/#/myAccount');
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']; // optional
$loginUrl = $helper->getLoginUrl(Mage::getBaseUrl().'facebook/facebook/authenticate', $permissions);
echo $loginUrl;
}
public function authenticateAction(){
Mage::log("Authenticate=======");
$fb = new Facebook\Facebook([
'app_id' => '934741473308351',
'app_secret' => 'de001d018a7769eb17eb11300e772a0c',
'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)) {
// Logged in!
Mage::log("Access Token=================>".(string) $accessToken);
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$fb->setDefaultAccessToken((string) $accessToken);
$_SESSION['facebook_access_token'] = (string) $accessToken;
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
Mage::log("Email====>Before".$userNode->getFirstName());
$customer = $this->checkIfUserExists($userNode);
$this->_redirectUrl('/app-new/app/#/myAccount');
exit;
} 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;
}
}
}
I got the result first, but I am not getting result after that. Is it due to access taken or something else. What am I doing wrong here
You are missing a new feature of v2.4 of the Graph API, it´s called "Declarative Fields". You now have to add a field parameter to specify the fields you want to get, else you will only get id and name: https://developers.facebook.com/docs/apps/changelog#v2_4
Also, make sure your login works correctly and you get asked for the email permission when you authorize your App.
I'm using Facebook's redirect helper.
The helper works fine but error occurs at login-callback.php.
Here's my login-callback.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
require_once 'functions.php'; //a file I require
session_start();
$fb = new Facebook\Facebook([
'app_id' => 'xxxx',
'app_secret' => 'xxxx',
'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)) {
$_SESSION['facebook_access_token'] = (string) $accessToken;
try {
$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;
}
$user = $response->getGraphUser();
$name=$user['name'];
$id = $user->getId();
$email = $user->getEmail();
$id='f'.$id;
$result=queryMysql("SELECT * FROM user WHERE id='".$id."'");
if(!$result->num_rows)
{
$result=queryMysql("INSERT INTO user VALUES('$id','$name','$email')");
}
$_SESSION['logger']='facebook';
header('Location: http://localhost/fee/index.php');
}
?>
Graph api and $helper->accessToken gives me the following error.
Sometimes $helper->accessToken works and i receive access token but graph api never works.
The error I get is :
Facebook SDK returned an error: Failed to connect to graph.facebook.com port 443: Network is unreachable
I've searched all over for this, the 'answers' I got were outdated. Anytime I run the script it posts the message with my profile to the facebook page even though I have admin permissions.
This is the code I'm using
<?
require_once __DIR__ . '/Facebook/autoload.php';
session_start();
$fb = new Facebook\Facebook([
'app_id' => '',
'app_secret' => '',
'default_graph_version' => 'v2.4'
]);
$page_id="";
$accessToken='Access token I got from https://developers.facebook.com/tools/explorer/145634995501895/';
// Sets the default fallback access token so we don't have to pass it to each request
$fb->setDefaultAccessToken('');
try {
$response = $fb->get('/me');
$userNode = $response->getGraphPage();
} 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;
}
echo 'Logged in as ' . $userNode->getName();
?>