Facebook App: Where do i put offline_access request? - php

I need my app to request the offline_access permission (detailed here), but as ever I'm baffled by the Facebook documentations.
It says I need a comma separated list of my permission demands, like 'publish_stream,offline_access' etc
where do i put this list in the interface below????

It's used with the API, as follows
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => false,
));
$facebook_login_url = $facebook->getLoginUrl(array(
'next' => '',
'cancel_url' => '',
'req_perms' => 'email,publish_stream,status_update'
));
Where $facebook_login_url is the URL tha the user needs to follow to grant you access.
Does that help?

You can't do this with that interface. You need to set scope parameter when redirecting user to facebook.
x=y&scope=email,user_about_me,user_birthday,user_photos,publish_stream,offline_access

First define which permissions you will need:
$par['req_perms'] = "friends_about_me,friends_education_history,friends_likes,friends_interests,friends_location,friends_religion_politics,
friends_work_history,publish_stream,friends_activities,friends_events,
friends_hometown,friends_location,user_interests,user_likes,user_events,
user_about_me,user_status,user_work_history,read_requests,read_stream,offline_access,user_religion_politics,email,user_groups";
$loginUrl = $facebook->getLoginUrl($par);
Then, check if the user has already subscribed to app:
$session = $facebook->getSession();
if ( is_null($session) ) {
// no he is not
//send him to permissions page
header( "Location: $loginUrl" );
}
else {
//yes, he is already subscribed, or subscribed just now
echo "<p>everything is ok";
// write your code here
}

Related

Posting a comment on the news feed

Hey all i am using the following code to post to a posting on my news feed:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'fileUpload' => true,
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try {
$access_token = $facebook->getAccessToken();
$user_profile = $facebook->api('/me');
$comment = $facebook ->api('/xxxxxxxxxxxxxx/comments',
'POST',
array(
'access_token' => $access_token,
'message' => 'testing!'
)
);
} catch (FacebookApiException $e) {
echo ($e);
$user = null;
}
}
<?php if ($user): ?>
Logout
<?php else: ?>
Login with Facebook
<?php endif ?>
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$params = array(
'scope' => 'read_stream, friends_likes, email, read_mailbox, read_requests, user_online_presence, friends_online_presence, manage_notifications, publish_actions, publish_stream, user_likes, user_photos, user_status, user_videos, read_insights'
);
$loginUrl = $facebook->getLoginUrl($params);
}
?>
<?php print_r($user_profile); ?>
For some reason i get this error:
OAuthException: (#221) Photo not visible
And i have no idea since i am posting a text comment and not even an image??
If i comment out the code line $comment = $facebook ->api('/xxxxxxxxxxxxxx/comments',
'POST',
array(
'access_token' => $access_token,
'message' => 'testing!'
)
); it works just fine (as in, displays my info with user_profile). I've tried reading over the page that tells you how to use the comments here and i do - it just doesn't seem to want to work?
What am i missing???
update
using the graph API i was able to do the same thing i am trying to do via PHP so i know it works...:
I know it's an old question but...
First, for tests the same enviroment, you have to use your app token, not the Graph API Explorer. Otherwise, you're testing the API with all the permissions.
Second, your problem must be a permission thing (that's why it's working on the API Explorer). You should:
Ask for publish_permission on the login (not recommended) or,
Ask for publish_permission when the user is ready to send the comment.
More information about this: Optimizing Permissions Requests
Here the permissions that you need: Publishing Comments
Permissions
As a general rule, when you see a OAuthException, you are having a permission issue.
If the post is photo type, you're going to need a user_photos permission.

Facebook App - auto login so can update status (php SDK)

I have built an app that allows users to update their status' externally. It uses the PHP SDK.
Is it possible to automatically login for registered users once they have initially authorised their facebook account via the app? I do not want them to have to login for every new session. Id like that once they have been authenticated the first time, they are always connected and not asked to login multiple times. Possibly by storing the access token in a database and this using this to connect somehow. Is this possible? I have noticed other apps that do this, but have no idea how they do this eg. ping.fm
Below is what I have so far, but this requires the user to select the "login" link when the session expires.
//instantiate the Facebook library with the APP ID and APP SECRET
$facebook = new Facebook(array(
'appId' => 'appid',
'secret' => 'secret',
'cookie' => true
));
//Get the FB UID of the currently logged in user
$user = $facebook->getUser();
//if the user has already allowed the application, you'll be able to get his/her FB UID
if($user) {
//start the session if needed
if( session_id() ) {
} else {
session_start();
}
//do stuff when already logged in
//get the user's access token
$access_token = $facebook->getAccessToken();
//check permissions list
$permissions_list = $facebook->api(
'/me/permissions',
'GET',
array(
'access_token' => $access_token
)
);
//check if the permissions we need have been allowed by the user
//if not then redirect them again to facebook's permissions page
$permissions_needed = array('publish_stream', 'read_stream', 'offline_access');
foreach($permissions_needed as $perm) {
if( !isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1 ) {
$login_url_params = array(
'scope' => 'publish_stream,read_stream,offline_access',
'fbconnect' => 1,
'display' => "page",
'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
);
$login_url = $facebook->getLoginUrl($login_url_params);
header("Location: {$login_url}");
exit();
}
}
//if the user has allowed all the permissions we need,
//get the information about the pages that he or she managers
$accounts = $facebook->api(
'/me',
'GET',
array(
'access_token' => $access_token
)
);
//save the information inside the session
$_SESSION['access_token'] = $access_token;
$_SESSION['accounts'] = $accounts['data'];
$facebookAuth = TRUE;
} else {
//if not, let's redirect to the ALLOW page so we can get access
//Create a login URL using the Facebook library's getLoginUrl() method
$login_url_params = array(
'scope' => 'publish_stream,read_stream,offline_access',
'fbconnect' => 1,
'display' => "page",
'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
);
$login_url = $facebook->getLoginUrl($login_url_params);
//redirect to the login URL on facebook
$facebook_login = $login_url;
}
if (isset($_GET['submit'])){
if ($_GET['facebook']){
//get the info from the form
$parameters = array(
'message' => $_GET['message']/*,
'picture' => $_POST['picture'],
'link' => $_POST['link'],
'name' => $_POST['name'],
'caption' => $_POST['caption'],
'description' => $_POST['description']*/
);
//add the access token to it
$parameters['access_token'] = $_SESSION['active']['access_token'];
//build and call our Graph API request
$newpost = $facebook->api(
'/me/feed',
'POST',
$parameters
);
if ($newpost){
echo 'posted to facebook';
} else {
echo 'not posted to facebook :0( ';
}
}
I did not check how ping.fm does it.
But after looking at the extended permissions list here:
http://developers.facebook.com/docs/reference/api/permissions/
xmpp_login (Provides applications that integrate with Facebook Chat the ability to log in users.)
Otherwise, i dont think its possible.
Even desktop Apps need to redirect the user to a browser for authentication.
This also may depend on whether the user has the "keep me logged in" checkbox marked on log-in.
Since you have the offline_access, then you basically can perform the actions regardless if the user is logged in or not.

PHP loop issues with facebook app auth

I am using the latest PHP-SDK(3.11) and i have issues when users come on my app for the first time. The application make infinite loops.
When the user have to give permissions to the application, he is redirected to :
https://www.facebook.com/connect/uiserver.php?app_id=**myappId**&method=permissions.request&display=page&next=http%3A%2F%2Fapps.facebook.com%2F**myApp**%2F&response_type=code&state=**theSate**&canvas=1&perms=user_birthday%2Cuser_location%2Cuser_work_history%2Cuser_about_me%2Cuser_hometown
and when he accept i have the following link returned :
http://apps.facebook.com/**myApp**/?error_reason=user_denied&error=access_denied&error_description=***The+user+denied+your+request.***&state=**theSate**#_
i don't understand why the access is denied when the user click on "allow".
if ($this->fbUser) {
.... Do Somthing
} else {
$this->loginUrl = $this->fb->facebook->getLoginUrl(array(
'scope' => implode(',', sfConfig::get('app_facebook_perms')
), 'next' => 'http://apps.facebook.com'. sfConfig::get('app_facebook_app_url')));
$this->logMessage($this->loginUrl, 'info');
sfConfig::set('sf_escaping_strategy', false);
}
<script type='text/javascript'>
top.location.href = "echo $this->loginUrl ";
</script>
Try something like this, since you need to store the access token, one way or another. Hard to know what you are doing (or not doing) from that snippet.
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'APP_ID_HERE',
'secret' => 'APP_SECRET_HERE',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getUser();
if(empty($session)) {
# There's no active session, let's generate one
$url = $facebook->getLoginUrl(array(
"response_type"=>"token", //Can also be "code" if you need to
"scope" => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos' ,
"redirect_uri"=> "http://test.com" //Your app callback url
));
header("Location: $url");
exit;
}
// user is logged in

Why am I getting a "the page isn't redirecting properly" error?

I got this from nettuts, can someone please tell me why am I getting a "the page isn't redirecting properly" error?
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'APP_ID_HERE',
'secret' => 'APP_SECRET_HERE',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getUser();
if(!empty($session)) {
# Active session, let's try getting the user id (getUser()) and user info (api->('/me'))
try{
$uid = $facebook->getUser();
# req_perms is a comma separated list of the permissions needed
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
));
header("Location: $url");
} catch (Exception $e){}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
You redirect authenticated user to $facebook->getLoginUrl(array(...)), creating a redirect loop.
You should redirect only unauthenticated users (redirect with req_perms should be in else clause). The redirect in try should happen only if you detect that user hasn't granted you all required permissions.
You can check granted permissions by invoking:
$perms = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT email,user_birthday,status_update,publish_stream,user_photos,user_videos FROM permissions WHERE uid=' . $facebook->getUser()
));
Modified code:
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'APP_ID_HERE',
'secret' => 'APP_SECRET_HERE',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getUser();
if(empty($session)) {
# There's no active session, let's generate one
$url = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos'
));
header("Location: $url");
exit;
}
// user is logged in
If you are using the latest PHP SDK, several changes have been made and the permissions you are asking do not work that way.
Here is an updated code.
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'APP_ID_HERE',
'secret' => 'APP_SECRET_HERE',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getUser();
if(empty($session)) {
# There's no active session, let's generate one
$url = $facebook->getLoginUrl(array(
"response_type"=>"token", //Can also be "code" if you need to
"scope" => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos' ,
"redirect_uri"=> "http://test.com" //Your app callback url
));
header("Location: $url");
exit;
}
// user is logged in
More info: http://developers.facebook.com/docs/authentication/

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