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.
Related
I used the facebook api to collect users facebook long-lived access token on my website and saved them in a database. now I want to retrieve them from the database and post on their wall. however I received the following error message:
Facebook SDK returned an error: You must provide an access token.
they have already given me the necessary permissions and I have provided access token from database
code:
<?php
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/src/Facebook/');
require_once(__DIR__.'/src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => '***************',
'app_secret' => '***************',
'default_graph_version' => 'v2.11',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email','publish_actions','manage_pages','publish_pages'];
// optional
$query = 'SELECT * FROM music_promotion';
$stmt = mysqli_query ($dbc,$query);
while ($row=mysqli_fetch_array($stmt)){
$_SESSION['facebook_access_token'] = $row['fb_access_token'];
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 (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// getting short-lived access token
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client-
>getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// setting default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
try {
//Post property to Facebook
$linkData = [
'link' => 'the link',
'message' => 'the message'
];
$pageAccessToken = $_SESSION['facebook_access_token'];
try {
$response = $fb->post('/me/feed', $linkData, $pageAccessToken);
} 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();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
// redirecting user back to app login page
header("Location: ./");
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}
}
I've this simply facebook login integration.
I need to pass to the callback (that in this case is this same page) a custom parameter but the new $helper->getLoginUrl does not allow adding params. Previuosly I see that there was a next parameter that can be used to store a string but now it was removed.
It seems that I can't add a custom param neither in the session.
Is there any method to achieve my purpose?
<?php
require '../facebooksrc/autoload.php';
$userdata='/me?fields=name,first_name,last_name,email,gender,location,birthday';
$permissions = ['email, user_location, publish_actions, user_birthday'];
$fb = new Facebook\Facebook([
'app_id' => $appid,
'app_secret' => $appsecret,
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
$_SESSION['FBRLH_state']=$_GET['state'];
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 (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
// getting short-lived access token
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
//$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
//$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// setting default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// redirect the user back to the same page if it has "code" GET variable
if (isset($_GET['code'])) {
header('Location: ./');
}
// getting basic info about user
try {
$profile_request = $fb->get($userdata);
$profile = $profile_request->getGraphNode()->asArray();
} 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;
}
$firstname=$profile["first_name"];
$lastname=$profile["last_name"];
// MY INSERT QUERY
} else {
$url="myurl";
$loginUrl = $helper->getLoginUrl($url, $permissions);
//REDIRECT
header("Location: $loginUrl");
}
As the title suggests, I am trying to execute a PHP file on linux command line and it is working fine, I am actually running PHP script to post a status on Facebook graph API, and on executing the script I am getting a webscript enclosed within a script tag,the response I get, how do I execute the response? how do I execute it automatically with out using a browser?
My final aim is to use the the cron tab to schedule the posting of a facebook status by calling this PHP script.
PHP CODE FOR POSTING
> $fb = new Facebook\Facebook([
'app_id' => '1784504901790451',
'app_secret' => '4b7b2673ecd268b962b0f25166955f23',
'default_graph_version' => 'v2.8',
]);
> $helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'publish_actions']; // optional
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 (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// validating the access token
try {
$request = $fb->get('/me');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
if ($e->getCode() == 190) {
unset($_SESSION['facebook_access_token']);
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl('http://localhost/AutoPHPfb/', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
exit;
}
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// posting on user timeline using publish_actins permission
try {
// message must come from the user-end
$data = [
'message' => 'My awesome photo upload example.',
'source' => $fb->fileToUpload('/var/www/bitvie.cloudapp.net/htdocs/AutoPHPfb/Photos/B612-2015-08-10-10-36-17.jpg'),
// Or you can provide a remote file location
//'source' => $fb->fileToUpload('https://example.com/photo.jpg'),
];
$request = $fb->post('/me/photos', $data);
$response = $request->getGraphNode();
} 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 $response['id'];
// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
} else {
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl('http://bitvie.cloudapp.net/AutoPHPfb/PHPprofilepic.php', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
}
and here is the reponse I get
<script>window.top.location.href='https://www.facebook.com/v2.8/dialog/oauth?client_id=1784504901790451&state=4a090fd3336bc798d3604bc760538e3f&response_type=code&sdk=php-sdk-5.0.0&redirect_uri=http%3A%2F%2Fbitvie.cloudapp.net%2FAutoPHPfb%2FPHPprofilepic.php&scope=email%2Cpublish_actions'</script>
Good Noon,
I am trying to create new facebook canvas app. Facebook app tried to get access token from getCanvasHelper() method, but every time I tried to print it shows blank. I am using PHP SDK 5.0.0
$helper = $fb->getCanvasHelper();
$accessToken = $helper->getAccessToken();
Here below is the full sample code. Due to access token issue I am unable to process further.
Any help would be appreciable.
Here below is my sample code.
<?php
session_start();
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'my_app_id_i_have_replce_it_for_security',
'app_secret' => 'my_app_secret_i_have_replce_it_for_security',
'default_graph_version' => 'v2.4',
]);
$helper = $fb->getCanvasHelper();
$permissions = ['user_posts']; // optionnal
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 (isset($accessToken)) {
if (isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
} else {
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
// validating the access token
try {
$request = $fb->get('/me');
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
if ($e->getCode() == 190) {
unset($_SESSION['facebook_access_token']);
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/bs-kapil-canvas/', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
exit;
}
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
// getting all posts published by user
try {
$posts_request = $fb->get('/me/posts?limit=500');
} 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;
}
$total_posts = array();
$posts_response = $posts_request->getGraphEdge();
if($fb->next($posts_response)) {
$response_array = $posts_response->asArray();
$total_posts = array_merge($total_posts, $response_array);
while ($posts_response = $fb->next($posts_response)) {
$response_array = $posts_response->asArray();
$total_posts = array_merge($total_posts, $response_array);
}
print_r($total_posts);
} else {
$posts_response = $posts_request->getGraphEdge()->asArray();
print_r($posts_response);
}
// Now you can redirect to another page and use the access token from $_SESSION['facebook_access_token']
} else {
$helper = $fb->getRedirectLoginHelper();
$loginUrl = $helper->getLoginUrl('https://apps.facebook.com/bs-kapil-canvas/', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
}
I would like to ask a facebook timeline and photo gallery, but the access token is null.
$this->FaceBookApp = new Facebook\Facebook(array(
"app_id" => $this->appID,
"app_secret" => $this->appSecret,
"default_graph_version" => "v2.5"
));
$helper = $this->FaceBookApp->getRedirectLoginHelper();
$permissions = array('user_posts');
$loginUrl = $helper->getLoginUrl( _WEBPAGE .'/facebook-sdk/printAccessToken.php', $permissions);
echo "<script>window.top.location.href='".$loginUrl."'</script>";
callback php:
$helper = $this->FaceBookApp->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;
}
var_dump($accessToken);
when i try without access token, the
$response = $this->FaceBookApp->get('/'.$userId.'/feed', $this->appID .'|'. $this->appSecret);
var_dump( $response->getDecodedBody() );
is empty array.
Please, help me.