How to get publish_stream permissions for facebook app? - php

I have an app which has been running for a while which has only requested some account information for login purposes. Now I want to use it to publish to the stream. I have added publish_stream to the req_perms but, it still doesn't ask for that permission.
Am I missing something?
<?php
# CREATE FACEBOOK BUTTON
$facebook = new Facebook(array(
'appId' => FACEBOOKAPPID,
'secret' => FACEBOOKSECRET,
'cookie' => false,
));
$fb_session = $facebook->getUser();
$fb_me = null;
// Session based API call.
if ($fb_session) {
try {
$fb_uid = $fb_session;
$fb_me = $facebook->api('/me');
$fb_me['photo'] = 'http://graph.facebook.com/'.$fb_uid.'/picture?type=large';
$_SESSION['login_api'] = 1;
$_SESSION['login_api_details'] = $fb_me;
$_SESSION['login_api_user_id'] = $fb_uid;
# WE ARE GOOD TO GO, LETS GET THE ACCESS TOKEN
$_SESSION['access_token'] = $facebook->getAccessToken();
#header_redirect(SITEURL.'/login');
} catch (FacebookApiException $e) {
error_log($e);
}
}
else{
# LOGIN URL FOR FACE BOOK & request extra stuff
$fb_login_url = $facebook->getLoginUrl(array('req_perms'=>'publish_stream,email,user_about_me,user_birthday,user_website'));
header_redirect($fb_login_url);
}
?>

Try this in your else
$params = array(
'canvas' => 1,
'scope' => 'publish_stream,email,user_about_me,user_birthday,user_website',
'fbconnect' => 1,
'redirect_uri' => 'https://apps.facebook.com/YOURAPP',
);
$fb_login_url = $facebook->getLoginUrl($params);
header_redirect($fb_login_url);

Related

Facebook: PHP SDK issues

I have some facebook php code that worked flawlessly before the 4/30/15 upgrade. I upgraded my code, it seems to work on certain computers but not on others. On computers that it doesn't work on I've tried multiple browsers with the same result. I am able to log into Facebook using the SDK, but it won't post anything to my page's wall. Same code, different computer, and everything works fine. Here's the code:
<?php
$facebook = new Facebook(array(
'appId' => '##########',
'secret' => '##########',
'fileUpload' => true
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array(
'next' => ($user['baseurl'] . 'logout.php')
));
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_pages , manage_pages'
));
}
$access_token = $facebook->getAccessToken();
$params = array(
'access_token' => $access_token
);
#The id of the fanpage
$fanpage = '############';
#The id of the album
$album_id = '############';
if ($user) {
$accounts = $facebook->api('/me/accounts', 'GET', $params);
foreach ($accounts['data'] as $account) {
if ($account['id'] == $fanpage || $account['name'] == $fanpage) {
$fanpage_token = $account['access_token'];
}
}
$message = 'Post this to the wall.';
$img = 'path to image.jpg';
$args = array(
'message' => $message,
'image' => '#' . $img,
'aid' => $album_id,
'no_story' => 0,
'access_token' => $fanpage_token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
}
?>
Most permissions need to get approved by Facebook before they can be used for any user, else they only work for users with a role in the App. Check out the docs about Login Review.
You can only use publish_pages and manage_pages as App Admin/Developer/Tester without review.
If you only test with those users and it still does not work, debug the Access Token in the Debugger and see if those permissions are really authorized: https://developers.facebook.com/tools/debug/
Edit: After reading your comment, i believe you are trying to use a User Token. Use /me/accounts to get a Page Token and try again with that one. If you debug the Token, make sure the Page shows up too.

Facebook re-authentication

I'm using PHP SDK to authentication and requesting permissions.
$facebook = new Facebook(array(
'appId' => '7xxx349xx',
'secret' => 'x0b360034c9exxxx4f',
'cookie' => true
));
$params = array(
'scope' => 'publish_actions, read_friendlists',
'redirect_uri' => 'example.com',
);
$user = $facebook->getUser();
$usertoken = $facebook->getAccessToken();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
header("Location: xxx.php");
}
if(!$user) {
//$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl($params);
}
It works well but if user delete app in privacy settings and try to login again I have errors:
CSRF state token does not match one provided.
OR
OAuthException: Error validating access token: The user has not authorized application [APPID]
How to fix it? I need additional code?
Thanks for help

Facebook redirect url to index.php

index.php
<?php
//facebook application
$fbconfig['appid'] = "32##########";
$fbconfig['secret'] = "ca2dc#############";
$fbconfig['baseurl'] = "http://localhost/sbs/fblogin/index.php";
//
if (isset($_GET['request_ids'])) {
//user comes from invitation
//track them if you need
}
//facebook user uid
try {
include_once "src/facebook.php";
}
catch (Exception $o) {
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'baseurl' => $fbconfig['baseurl'],
'cookie' => true
));
//Facebook Authentication part
$user = $facebook->getUser();
// We may or may not have this data based
// on whether the user is logged in.
// If we have a $user id here, it means we know
// the user is logged into
// Facebook, but we don’t know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos ,user_work_history'
));
$logoutUrl = $facebook->getLogoutUrl();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
// d is a debug function defined at the end of this file
$user = null;
}
}
//if user is logged in and session is valid.
if ($user) {
//get user basic description
$userInfo = $facebook->api("/$user?fields=picture,name,email,gender,birthday");
//$pic = $facebook->api("/$user/pictures");
$profile = json_encode($userInfo);
$res = json_decode($profile, true);
$_SESSION['name'] = $res['name'];
$_SESSION['email'] = $res['email'];
$_SESSION['id'] = $res['id'];
$_SESSION['gender'] = $res['gender'];
$_SESSION['birthday'] = $res['birthday'];
$_SESSION['img'] = $res['picture']['data']['url'];
$_SESSION['auth_type'] = "facebook";
if (isset($_COOKIE['registration']) && $_COOKIE['registration'] == true) {
header("location:../sbs/registration.php");
} else {
header("location:../sbs/sbs_login.php");
}
}
?>
For the 1st time I am working on the Facebook app. I have made app on the Facebook developer. It's working properly but it is not redirecting me in the index.php. I want to redirect it in this page only so all the values are stored in the session and I am checking it if the cookies is made then this value is going to registration.php and if not then its going to sbs_login.php. Please can anybody tell where to give the redirect url?
$loginUrl = $facebook->getLoginUrl(array(
'baseurl' => $fbconfig['baseurl'],
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos ,user_work_history'
));
I find the solution of my problem . so this is the solution
$loginUrl = $facebook->getLoginUrl(array(
'baseurl' => $fbconfig['baseurl'],
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos ,user_work_history'

Facebook backend login

I have been using this same code for so. I tried to use it and it will not let me connect. The front-end javascript one works and my app ids and all that match. Since i want to log in the user i do back end verification (as any sane person would) and in verifyfacebook.php (the stuff your looking at) all the includes are there and i verified that FACEBOOK_APP_ID and SECRET both are correct and they echo out with their values.
I just do not understand what is going on!
Anyone see anything i am just missing?
try {
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET_ID,
'cookie' => true,
));
$session = $facebook->getSession();
$fbme = null;
// Session based graph API call.
if ($session) {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $f) {
}
}
} catch (Exception $e) {
}
I just tried to simply the example and it still wont work.
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET_ID,
'cookies' => true
));
// Get User ID
$uid = $facebook->getUser();
echo $uid;
It echos '0'
cookie parameter is no longer used with 3.0.x. Below code did not work until I have removed all cookies, including session ones. redirect_uri is optional, just make sure you get redirected to the same page this code is executed.
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET_ID,
));
$user = $facebook->getUser();
if ( $user )
{
echo sprintf(
'Logout',
$facebook->getLogoutUrl(array(
'redirect_uri' => URL,
))
);
}
else
{
echo sprintf(
'Login',
$facebook->getLoginUrl(array(
'redirect_uri' => URL,
'scope' => SCOPE,
))
);
}
if ( $user )
{
try
{
print_r($facebook->api('/me');
}
catch ( FacebookApiException $e )
{
print_r($e);
}
}
This post should help:
http://developers.facebook.com/blog/post/525/
Try and implement the code as is, then once you get it working you can customise it to your needs.

Facebook Iframe application authentication?

I have developed a Facebook application that runs inside an iframe in the Facebook canvas. For it to work properly I request extended permissions from the user. If the user hasn't authorized the application I send him/her to a login page with the getLoginUrl() method in the PHP SDK.
It works, but it's not pretty. The method sends the user to a landing page before the authentication page. It looks like this:
When I click "Go to Facebook.com" I see the actual page for permission requests (I also get right to the permissions page if I print the url, copy it and enter it into a new browser window). How do I make Facebook skip this step when I do the redirect from an Iframe?
My code looks like this (using CodeIgniter and Facebook PHP SDK):
$this->facebook = new Facebook(array(
'appId' => '{MY_APP_ID}',
'secret' => '{MY_SECRET}',
'cookie' => TRUE,
'domain' => $_SERVER['SERVER_NAME']
));
$this->facebook->getSession();
try {
$this->me = $this->facebook->api('/me');
}
catch (FacebookApiException $e) {
$this->me = NULL;
}
if ( is_null($this->me) ) {
redirect($this->facebook->getLoginUrl(array(
'req_perms' => 'offline_access,read_stream,publish_stream,user_photos,user_videos,read_friendlists',
'next' => $this->config->item('base_url').'fblogin.php?redirect_uri='.$this->uri->uri_string()
)));
}
I think you need to redirect the parent frame (i.e. _top) rather than the iFrame itself?
The way I do it is set up an INDEX.PHP file with the following
//if user is logged in and session is valid.
if ($fbme){
//fql query example using legacy method call and passing
parameter
try{
$fql = "select name, hometown_location, sex,
pic_square from user where uid=" .
$uid;
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => 'http://apps.facebook.com/yoursite/'
);
$fqlResult = $facebook->api($param);
}
catch(Exception $o){
d($o);
}
}
Then point your canvas url to http://yoursite.com/INDEX.php
The callback url in the above code which will be in INDEX.PHP sets where to look after permissions are granted.
FBMain.php looks like this
//set application urls here
$fbconfig['http://www.yoursite.com/iframeapp/YOURMAINPAGE.php/']
= "http://www.tyoursite.com/YOURMAINPAGE.php/";
$fbconfig['http://apps.facebook.com/CANVASBASEURL']
= "http://apps.facebook.com/CANVASBASEURL";
$uid = null; //facebook user id
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['APPID'],
'secret' => $fbconfig['SECRET'],
'cookie' => true,
));
//Facebook Authentication part
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms'=>'email,publish_stream,status_update,user_birthday,user_location'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href
= '$loginUrl';";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href
= '$loginUrl';";
exit;
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
} ?>
Hope its a little clearer. It took me a while to figure it out, but I got there, thought I would help.

Categories