Facebook Extended permissions in canvas via php - php

I'm working on my new project, Woobook. I used the Old REST API until now but i should learn to work with latest one (with OAuth). First i want to get extended permissions but the attached code redirect me to a wrong page (instead of permissions dialog directly).
My app link: http://apps.facebook.com/woobook/
and the source code:
<?php
// Facebook API inc
require_once "inc/facebook.php";
// API init
$cid = "162513840463126";
$asi = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $cid,
'secret' => $asi,
'cookie' => true,
));
// Session
$session = $facebook->getSession();
if(!$facebook->getUser()) {
header("Location:".$facebook->getLoginUrl(array("next" => "http://apps.facebook.com/woobook/", "canvas" => 1, "req_perms" => "user_status,publish_stream,user_photos"))."");
exit;
}
?>

You're using an iframe app, which means you need to do this a little differently.
See my answer here

Related

Facebook API - Get Comment with php sdk error?

I have tried to get facebook comments, but always displays {"data":[]} , but in tools graph explorer, work perfectly
i'm using php-sdk
this my code
<?php
require 'inc/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true,
));
$user = $facebook->getUser();
if($user) {
$get = $facebook->api("/807642829246790/comments","GET");
print_r($get);
} else {
header("Location:".$facebook->getLoginUrl());
}
?>
so, what wrongs above code ?
This is because you are using the app token instead of user token. Try copying the token directly from graph explorer and use it to instantiate a new FB instance. Then retrieve comments from that instance. You would see the results as seen in graph explorer.

Facebook API and PHP - getLoginUrl() - never shows dialog and breaks on other FB account

I'm using the Facebook API and it's doing strange things - I have two issues:
When I use getLoginUrl(...) - I never get a popup - I don't click anything and it just redirects.
Using my FB account this code redirects and i see the 'user' and 'token' but when I try the same thing on a workmates FB account it gives the error "An error occurred. Please try again later".
Here's my code for the redirector:
require_once '../../src/facebook.php';
define('FACEBOOK_APP_ID',"xxx");
define('FACEBOOK_SECRET',"xxxxx");
define('REDIRECT_URI',"http://dev.example.com.au/php/work/redirectSimple.php");
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
$user = $facebook->getUser();
if($user == 0)
{
$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI));
echo ("<script> top.location.href='".$login_url."'</script>");
}
else
{
echo ("<script> window.location.href='".REDIRECT_URI."'</script>");
}
And my redirected code ( redirectSimple.php ):
echo "\n WELCOME!";
require_once '../../src/facebook.php';
$facebook = new Facebook(array(
'appId' => "xxx",
'secret' => "xxxxx",
'fileUpload' => true,
'cookie' => true
));
$user = $facebook->getUser();
echo "\n user = ".$user;
$token = $facebook->getAccessToken();
echo "\n token = ".$token;
I've also tried these things on another machine with the same outcome.
So, Maybe it's an account or App settings issue?
Firstly, don't go looking for a popup with PHP, unless you are using Ajax, as PHP doesn't do popups, being server side. If you want the popup you need to use the JavaScript SDK.
Secondly, after a successful login, you are redirecting to another file. Personally I would use require_once rather than a redirect, as you have already created a Facebook object and if you redirect you will have to recreate it.
If you couple that with "try" and "catch" you can trap any Facebook errors which will give you a clue as to what is going on.
Other than that, as the other person said, check you aren't in sandbox mode in the app settings.
There's no code here to create a 'popup' - getLoginUrl() creates a url you can use as part of the server side flow - you need to redirect to this url.
Your co-worker is most likely not a developer or tester of your app, and so cannot see it (it's probably in sandbox mode)

Facebook and php - how to post content from my site to my facebook wall?

I'm very confused with this facebook api.
I've facebook username and password in my database, I want to sign and post content from my site to my facebook wall. how do I do it?
I've downloaded the facebook-php-sdk, just need to be able to include the "post" part in my php file to make things happen, I've this code at the moment
require_once ('facebook.php');
$app_id = "MY_ID";
$app_secret = "MY_SECRET";
$facebook = new Facebook(array('appId' => $app_id, 'secret' => $app_secret, 'cookie' => true));
if(is_null($facebook->getUser())) {
header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
exit;
}
$status = $facebook->api('/me/feed', 'POST', array('message' => utf8_encode($my_message)));
with this I just get a internal server error, nothing more to have an idea of what I'm doing wrong...
even with var_dump($status) I get nothing
Thanks
You cannot log in as a user on the Facebook Graph API. You should never be asking for any user's Facebook password in the first place, which is why Facebook do not allow this.
The API allows you to log in as an 'app', which can be granted permissions by Facebook users. You will need to create a Facebook app at https://www.facebook.com/developers/apps.php.
Once this is done, you can prompt a user to post on their wall or use the Graph API using http://developers.facebook.com/docs/reference/dialogs/feed/
Firstly try (i hope you entered your $app_id and $app_secret):
require_once ('facebook.php');
$app_id = "MY_ID";
$app_secret = "MY_SECRET";
$facebook = new Facebook(array('appId' => $app_id, 'secret' => $app_secret, 'cookie' => true));
$user = $facebook->getUser();
var_export($user);
You should se data if you're logged in properly.
Than you should maybe look at this page for an example Facebook Graph API PHP SDK posting on page as page

Facebook login/connect help - PHP

I am having problems with Facebook connect.
Once I have the connect with Facebook button how do i retrieve the info with php?
There are loads of examples in developer.facebook.com but I couldn't get any of them to work.
Is it POST or GET or what? (I'm kinda new to php... )
Thanks.
DEMO: http://so.devilmaycode.it/facebook-login-connect-help-php/
Setup new App here
Download the PHP SDK here
Copy & Paste the code you'll find here into a file called index.php
Edit the first lines of the code above and provide the needed informations like below:
require 'sdk/facebook.php'; //the path to the downloaded PHP SDK
$facebook = new Facebook(array(
'appId' => '105810212821284', //App ID you find once created the app
'secret' => '3d6fdaa377cd4ca9...', //Secret Key you find once created the app
'cookie' => true,
));
you have done ;)
Application Settings:
Web Site -> Site URL -> http://so.devilmaycode.it/facebook/
Web Site -> Site Domain -> so.devilmaycode.it
Facebook Integration -> Canvas URL -> http://so.devilmaycode.it/facebook/
Requiriment
PHP5
CURL Lib
Check if you have the requirement
<? echo phpinfo(); ?>
Demo Source
http://pastebin.com/KyQ3CHV0
Use https://github.com/facebook/php-sdk/
Example
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
Display the data
echo $me['email']
http://developers.facebook.com/docs/reference/api/user/
More info about Graph API
To get the email or aditional data from users, you need to request it first(if not you only can get the basic data, like name / picture or only the public data), check the api facebook file in the function called "getLoginUrl".
'api_key' => $this->getAppId(),
'cancel_url' => $currentUrl,
'display' => 'page',
'req_perms' => 'email', <------------- email
'fbconnect' => 1,
'next' => $currentUrl,
'return_session' => 1,
'session_version' => 3,
'v' => '1.0',

is there proper codeigniter library to work with facebook php sdk

I have searched Google for Libraries for Facebook, and have found this one: http://www.haughin.com/code/facebook/,
but it seems a bit outdated.
I wanted something for this one: https://github.com/facebook/php-sdk/
I have written my own wrapper for it meanwhile, but it seems like I'm having some issues with $_REQUEST['...']
You can use Official PHP Facebook SDK in Codeigniter easily. The only problem is Facebook SDK needs $_REGISTER and in Codeigniter you don't have it.(Because of the mod_rewrite)
Here's the little solution:
1- This is for the $_REGISTER problem. Use this before loading Facebook class:
parse_str( $_SERVER['QUERY_STRING'], $_REQUEST );
2- Facebook SDK has 2 files. Put them into one file and save it to your application/helper folder as facebook_helper.php. Then loading and using is such an ease like this:
$this->load->helper( 'facebook' );
$facebook = new Facebook( array( 'appId' => 'XXX', 'secret' => 'xxx' ) );
// ... The rest is not different. You can read Facebook SDK examples
So here's a trick I used to use facebook PHP sdk with my CodeIgniter app. From the SDK code, take Facebook.php and take out the FacebookApiException class, and put it in a new file called FacebookApiException.php. Now, I put facebook.php and FacebookApiException.php into the models folder, and used them as regular models.
Here is the code I used for authenticating users and providing access to an application via Facebook.
function facebook_login(){
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXX',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getSession();
if(!empty($session)) {
# Active session, let's try getting the user info
try{
$uid = $facebook->getUser();
$param = array(
'method' => 'users.getinfo',
'uids' => $uid,
'fields' => 'uid, username, name, profile_url, pic_big',
'callback' => ''
);
$user = $facebook->api($param);
} catch (Exception $e){
$url = $facebook->getLoginUrl(array(
'req_perms' => 'user_about_me, email, status_update, publish_stream, user_photos',
'next' => site_url() . 'user/facebook_login',
'cancel' => site_url()
));
redirect($url);
}
if(!empty($user)){
# User info ok?
print_r($user);
// Add user oauth token and info to DB, and then redirect to some controller in your application.
redirect('/'); // redirect to homepage
} else {
# For testing purposes, if there was an error, let's kill the script
die("There was an error.");
}
} else {
# There's no active session, let's generate one
$url = $facebook->getLoginUrl(array(
'req_perms' => 'user_about_me,email,status_update,publish_stream,user_photos',
'next' => site_url() . 'user/facebook_login',
'cancel' => site_url()
));
redirect($url);
}
}
Hope this helps.
You can handle it in clear way by writing a small library by inheriting Facebook SDK class. Please follow my post on http://www.betterhelpworld.com/codeigniter/how-to-use-facebook-php-sdk-v-3-0-0-with-codeigniter to get it working.
There is also Facebook-Ignited.

Categories