SDK 4.0 Facebook, no dialog to request permission publish_actions - php

I created an app on facebook and via PHP SDK 4.0 I created the login and I need to have write permissions on the feed, but even we have added the permissions, when I do not log apparre the more window box to approve and grant permissions .
this is the code used:
<?php session_start();
require 'Facebook/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('XXXXXXXXXX XXXXXXXX','XXXXXXXXXXXXXXXXXX');
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'XXXXXXXXXXXXX' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo print_r( $graphObject, 1 );
if($session) {
try {
$response = (new FacebookRequest(
$session, 'POST', '/me/feed', array(
'link' => 'www.example.com',
'message' => 'User provided message'
)
))->execute()->getGraphObject();
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
} else {
$params = array(
scope => 'publish_actions'
//redirect_uri => $url
);
// show login url
echo 'Login';
}
?>

publish_actions should be approved by facebook for your application if you have already submitted for approval facebook might take upto a week till then you can't publish stories on facebook user's timeline.

Related

Exception occured, code: 200 with message: (#200) Permissions error

I have a problem with FB PHP SDK.
Here is my code:
require_once("autoload.php"); // set the right path
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphObject;
use Facebook\FacebookRequestException;
$APP_ID = 'APP_ID';
$APP_SECRET = 'APPSECRET';
$TOKEN = "TOKEN";
$ID = "PAGE_ID"; // your id or facebook page id
FacebookSession::setDefaultApplication($APP_ID, $APP_SECRET);
$session = new FacebookSession($TOKEN);
$params = array(
"message" => "Xoşbəxt olmamaq haqsızlıq deyilmi özümüzə qarşı?!"
);
if ($session) {
try {
$response = (new FacebookRequest(
$session, 'POST', '/' . $ID . '/feed', $params
))->execute()->getGraphObject();
echo "Posted with id: " . $response->getProperty('id');
} catch (FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
But when i try to run this code it returns an error:
Exception occured, code: 200 with message: (#200) Permissions error
in some forums i read that may be i need to approve premissons/ but when i submited premissions to FB they answered that if i am using it only for my purpose (as app admin) premisson approval is not requred.
What can be problem?
Debug your Access Token: https://developers.facebook.com/tools/debug/
Make sure it includes the manage_pages and publish_pages permission. And make sure you are using a Page Token. More information about the different Tokens:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

how to implementation "graph.facebook" in php file

i want to use http://graph.facebook.com/XXX in my site
how i do a php file that have use this system and show my facebook details when i enter the link?
XXX = userrname
i want that the site show the details of the facebook user that enter the site
i tried everything but nothing, i dont want to make ap like this:
<?php
session_start();
require_once 'autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
$api_key = 'FACEBOOK_APP_ID';
$api_secret = 'FACEBOOK_APP_SECRET';
$redirect_login_url = 'http://www.yoursite.com/somefolder/file.php';
// https://www.webniraj.com/2014/05/01/facebook-api-php-sdk-updated-to-v4-0-0/
// initialize your app using your key and secret
FacebookSession::setDefaultApplication($api_key, $api_secret);
// create a helper opject which is needed to create a login URL
// the $redirect_login_url is the page a visitor will come to after login
$helper = new FacebookRedirectLoginHelper( $redirect_login_url);
// First check if this is an existing PHP session
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from the existing PHP sesson
$session = new FacebookSession( $_SESSION['fb_token'] );
try {
// validate the access_token to make sure it's still valid
if ( !$session->validate() ) $session = null;
} catch ( Exception $e ) {
// catch any exceptions and set the sesson null
$session = null;
echo 'No session: '.$e->getMessage();
}
} elseif ( empty( $session ) ) {
// the session is empty, we create a new one
try {
// the visitor is redirected from the login, let's pickup the session
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $e ) {
// Facebook has returned an error
echo 'Facebook (session) request error: '.$e->getMessage();
} catch( Exception $e ) {
// Any other error
echo 'Other (session) request error: '.$e->getMessage();
}
}
if ( isset( $session ) ) {
// store the session token into a PHP session
$_SESSION['fb_token'] = $session->getToken();
// and create a new Facebook session using the cururent token
// or from the new token we got after login
$session = new FacebookSession( $session->getToken() );
try {
// with this session I will post a message to my own timeline
$request = new FacebookRequest(
$session,
'POST',
'/me/feed',
array(
'link' => 'www.finalwebsites.com/facebook-api-php-tutorial/',
'message' => 'A step by step tutorial on how to use Facebook PHP SDK v4.0'
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
// the POST response object
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
$msgid = $graphObject->getProperty('id');
} catch ( FacebookRequestException $e ) {
// show any error for this facebook request
echo 'Facebook (post) request error: '.$e->getMessage();
}
// now we create a second request to get the posted message in return
if ( isset ( $msgid ) ) {
// we only need to the sec. part of this ID
$parts = explode('_', $msgid);
try {
$request2 = new FacebookRequest(
$session,
'GET',
'/'.$parts[1]
);
$response2 = $request2->execute();
$graphObject2 = $response2->getGraphObject();
// the GET response object
echo '<pre>' . print_r( $graphObject2, 1 ) . '</pre>';
} catch ( FacebookRequestException $e ) {
// show any error for this facebook request
echo 'Facebook (get) request error: '.$e->getMessage();
}
}
} else {
// we need to create a new session, provide a login link
echo 'No session, please login.';
}
// use this for testing only
//unset($_SESSION['fb_token']);
thanks,
mtj

Post image on facebook user's timeline

I have an app which uses the Facebook PHP API. This is part of my code ,I am not sure what i'm doing wrong or what i'm missing at the moment. I can't seem to post an image on the user's timeline on facebook. Any help would be much appreciated. Many Thanks.
<?php
echo "<p> Hello World!</p>";
// php 5.3 and up can throw an error if this is not set
date_default_timezone_set("Europe/London");
// much of the example code on the web forgets to include these HttpClients, for some reason
require_once( '../docs/Facebook/HttpClients/FacebookHttpable.php' );
require_once( '../docs/Facebook/HttpClients/FacebookCurl.php' );
require_once( '../docs/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( '../docs/Facebook/FacebookSession.php' );
require_once( '../docs/Facebook/FacebookRedirectLoginHelper.php' );
require_once( '../docs/Facebook/FacebookRequest.php' );
require_once( '../docs/Facebook/FacebookResponse.php' );
require_once( '../docs/Facebook/FacebookSDKException.php' );
require_once( '../docs/Facebook/FacebookRequestException.php' );
require_once( '../docs/Facebook/FacebookAuthorizationException.php' );
require_once( '../docs/Facebook/GraphObject.php' );
// This one is also often left out
require_once( '../docs/Facebook/Entities/AccessToken.php');
// store your $HOSTNAME, $APPID and $SECRET in this file:
require_once( '../docs/my_app_credentials.php' );
session_start();
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication($APPID,$SECRET);
// login helper with redirect_uri
$PAGENAME="page2_upload_photo.php";
$REDIRECT_URL="http://".$HOSTNAME.'/'.$PAGENAME;
$IMAGE_FILENAME='#images/Flower.jpg';
$IMAGE_MESSAGE='hello flower';
$helper = new FacebookRedirectLoginHelper( $REDIRECT_URL );
echo "<p> the redirect url is ".$REDIRECT_URL."</p>";
try {
// echo "<p> about to try to get session: the helper variable: </p>";
// var_dump($helper);
$session = $helper->getSessionFromRedirect();
// echo "<p> the session variable:</p>";
// var_dump($session);
}
catch( FacebookRequestException $ex ) {
// When Facebook returns an error
echo "<p> There was a facebook request exception</p>";
}
catch( \Exception $ex ) {
echo "<p> There was a validation failure</p>";
// When validation fails or other local issues
}
// see if we have a session
if ( isset( $session ) ) {
echo "<p> Now try to upload a photo to the album /me</p>";
try {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
//$myCurlFile = new CURLFile('#./'.$IMAGE_FILENAME, 'image/jpg');
$myPhoto = "#images/Flower.jpg";
//var_dump($myCurlFile);
$response = (new FacebookRequest(
$session, 'POST', '/me/photos',
array(
'source'=>'$IMAGE_FILENAME',
'message'=>'$IMAGE_MESSAGE'
)
))->execute()->getGraphObject();
// If you're not using PHP 5.5 or later, change the file reference to:
// 'source' => '#/path/to/file.name'
echo "Posted with id: " . $response->getProperty('id');
} catch(FacebookRequestException $e) {
echo "Exception occurred, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
} else {
// show login url
echo 'Login';
}
?>
You haven't set the upload file.
$response = (new FacebookRequest(
$session, 'POST', '/me/photos',
array(
'source'=>'$IMAGE_FILENAME',
'message'=>'$IMAGE_MESSAGE'
)
))->execute()->getGraphObject();
You enclosed the variables in quotes which will make it a string, so source is getting the literal string $IMAGE_FILENAME
Here is a simple example
$IMAGE_FILENAME='#images/Flower.jpg';
$IMAGE_MESSAGE='hello flower';
$arr = array(
'source'=>'$IMAGE_FILENAME',
'message'=>'$IMAGE_MESSAGE'
);
$correct_arr = array(
'source'=>$IMAGE_FILENAME,
'message'=>$IMAGE_MESSAGE
);
print_r($arr);
print_r($correct_arr);
Response
Array
(
[source] => $IMAGE_FILENAME
[message] => $IMAGE_MESSAGE
)
Array
(
[source] => #images/Flower.jpg
[message] => hello flower
)

Retrieving email from Facebook GraphObject

I am not able to retrieve the email from the Graph Object. I see on my app that I have permission for it. Here is my current code:
FacebookSession::setDefaultApplication('xxx','xx');
$helper = new FacebookRedirectLoginHelper('xxx');
$session = null;
try {
$session = $helper->getSessionFromRedirect();
Core::session('FacebookAuthSession')->sessionObject = $session;
} catch(FacebookRequestException $ex) {
} catch(\Exception $ex) {
}
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
$graphObject = $response->getGraphObject();
You can use following code to execute:
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
if($session) {
try {
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo "Email: " . $user_profile->getEmail();
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
To test results, you can also use explorer: https://developers.facebook.com/tools/explorer
The permissions are just listed as "approved without review" in your App settings, but you still have to authorize a user with them by using the "scope" parameter in the login process: https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/

Integrating facebook php sdk in Facebook Canvas App giving Blank page or oauthData error

I am trying to integrate facebook for my canvas app. When i run app from facebook with following code
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('xx','xx');
$helper = new FacebookCanvasLoginHelper();
try {
$data = array('oauth_token' => 'token');
$data['algorithm'] = 'HMAC-SHA256';
$data['issued_at'] = time();
$base64data = base64_encode(json_encode($data));
$rawSig = hash_hmac('sha256', $base64data, 'app_Secret', true);
$sig = base64_encode($rawSig);
$signedRequest = $sig.'.'.$base64data;
$_GET['signed_request'] = $signedRequest;
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
echo $ex;
} catch(\Exception $ex) {
echo $ex;
}
The entire page just turns blank white because of $_GET['signed_request'] = $signedRequest;.
What should I do to get login. If i just do $session = $helper->getSession(); instead of Get i get invalid signed paramters oAuth data missing.
Your PHP should be:
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
if($session){
try {
$facebook_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
echo $facebook_profile->getName;
} catch(FacebookRequestException $e) {
}
}
} catch(FacebookRequestException $ex) {
echo $ex;
} catch(\Exception $ex) {
$facebookLoginHtml = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id={your_app_id}&redirect_uri={your_app_canvas_url}';";
}
And then somewhere in your HTML:
<script>
<?php if(isset($facebookLoginHtml)){ echo $facebookLoginHtml; } ?>
</script>
If you want to ask for extra permission, add the scope parameter in the URL like this:
$facebookLoginHtml = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id={your_app_id}&redirect_uri={your_app_canvas_url}&scope=publish_actions';";
That will redirect the page to the login page, and then come back to your canvas app with the proper permission.
This shouldn't work like this as it's using Javascript with the PHP SDK. It's a bug that is being addressed by Facebook which you can follow here:
https://developers.facebook.com/bugs/722275367815777
I'll edit the answer if that bug ever gets resolved.
Thanks guys!
My approach:
<?php
session_start();
require ({your_php_sdk_path} . 'autoload.php');
use Facebook\FacebookCanvasLoginHelper;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
FacebookSession::setDefaultApplication({your_app_id},{your_app_secret});
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
}catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues
}
if (!is_null($session)) {
// Logged in
try {
//Get user name
$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
$user_profile_name = $user_profile->getName();
//Get user picture
$request = new FacebookRequest(
$session,
'GET',
'/me/picture',
array (
'redirect' => false,
'height' => '135',
'width' => '135',
)
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
$user_profile_picture = $graphObject->getProperty('url');
} catch(FacebookRequestException $e) {
// When Facebook returns an error
} catch(Exception $e) {
// When validation fails or other local issues
}
}else{
//First time -> ask for authorization
$helper = new FacebookRedirectLoginHelper({your_canvas_url});
$login_url = $helper->getLoginUrl();
}
?>
And in your html put a javascript:
<script type="text/javascript">
if($login_url != null){
top.location.href = $login_url;
}
</script>
<?php
// init app with app id (APPID) and secret (SECRET)
FacebookSession::setDefaultApplication('*********','*********' );
$helper = new FacebookCanvasLoginHelper();
try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
} catch(Exception $ex) {
// When validation fails or other local issues
}
if($session) {
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
// print data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
}
else {
// show login url
echo 'Login';
}
?>

Categories