How to request scope/permission from Facebook during user registration - php

I am using the information on http://developers.facebook.com/docs/plugins/registration/ to register, and login users with Facebook. How would I request this permission? I've tried to use
&scope=permission,permission,etc,etc
before fields, and then after fields. Regardless, the system still says
The user hasn't authorized the application to perform this action
Any help on this issue would be much appreciated!
After registration, I try as follows and it still spit out the same error:
if ($_REQUEST) {
require 'facebook.php';
$facebook = new Facebook(array('appId' => APP_ID,'secret' => APP_SECRET));
$user = $facebook->getUser();
if ($user) {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'email,publish_stream'));
$statusUpdate = $facebook->api('/me/feed', 'post', array('message'=> 'Testing posting to may wall', 'cb' => ''));
} else {
$loginUrl = $facebook->getLoginUrl(
array('scope' => 'email,publish_stream','redirect_uri' => REDIRECT_URL )
);
}
}

You need to request permissions after registration seperately. There is no way to request permissions through registration plugin at the moment.
This feature is on the wishlist:
http://bugs.developers.facebook.net/show_bug.cgi?id=14733

Related

How to fix "(#200) The user hasn't authorized the application to perform this action" error while posting on facebook wal

I am getting "(#200) The user hasn't authorized the application to perform this action" error when posting message on user's facebook wall. I am using facebook graph API. I have also set the "publish_actions" permission in the scope, but didn't working. My code is
require 'facebook/facebook.php';
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
));
$user = $facebook->getUser();
if ($user) {
$accessToken = $facebook->getAccessToken();
try {
//create message with token gained before
$post = array(
'access_token' => $accessToken,
'message' => 'Test message'
);
$res = $facebook->api('/me/feed', 'POST', $post);
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
# There's no active session, let's generate one
$login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_actions'
));
header("Location: " . $login_url);
}
Can you please suggest me what i am missing in the code or in the app?
Since you mentioned that it does work for Admins, but not for anyone else, it´s very clear what the problem is: You need to go through Login Review with publish_actions - else, that permission will only work for users with a role in the App.
All the information you need about Login Review is in the docs: https://developers.facebook.com/docs/facebook-login/review

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 PHP SDK - How to get access token?

I'm trying to post on user's Facebook wall from my app. The user granted the permissions to the app to post on his wall, and I have userid in db. I need to send the post automatically, without the user logging in again.
My code is:
try{
require_once(dirname(__FILE__) . '\lib\facebook-php\src\facebook.php' );
}
catch(Exception $o){
print_r($o);
}
$config = array(
'appId' => '123456',
'secret' => '454544',
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_id = '123456';
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'access_token' => $facebook->getAccessToken(),
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
// Give the user a logout link
echo '<br />logout';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
var_dump($e);
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
This throws An active access token must be used to query information about the current user. error and asks for login. After I click login, the post is sucesfully posted.
How can I get the access token with userid without the user logging in?
This happens when the active access token is not available while calling the API end point /me
There are two solutions to get rig of this
use the userid instead of /me
set the access-token prior sending the api call
For the first you can make the call as
ret_obj = $facebook->api('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
You dont need to send 'access_token' => $facebook->getAccessToken(),
The Second option is to do as
$access_token = $facebook->getAccessToken();
$facebook->setAccessToken($access_token);
ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
NOTE: publish_stream scope must be send while doing the login
Deprecation Notice :publish_stream was replaced by publish_actions, check the facebook doc for permission
you have to get permission .
Check it out here
https://developers.facebook.com/docs/reference/login/

Facebook Cancelled Permissions Redirect

So I'm having a bit of trouble when a user declines an app's permissions.
When the user accepts the permissions, they are redirected to the app just fine. When the user clicks Cancel/Decline, it should redirect them to the main Page, but instead it loops and asks them to accept the permissions again.
I am using the PHP API and the following is my code:
// Development
$app_id = 'XXXXXXXX';
$app_secret = 'YYYYYYYYYY';
$app_url = 'LINK_TO_FACEBOOK_PAGE_TAB';
$cancelUrl = 'LINK_TO_FACEBOOK_PAGE';
$GRAPH_URL = "https://graph.facebook.com/";
$scope = 'publish_actions';
// Init the Facebook SDK
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get the current user
$user = $facebook->getUser();
// Get the users token
$token = $facebook->getAccesstoken();
// If the user has not installed the app, redirect them to the Login Dialog
if(isset($_REQUEST['error']))
{
if(isset($_REQUEST['error_reason']) && $_REQUEST['error_reason']=='user_denied')
{
echo "<script>top.location.href='{$cancelUrl}'</script>";
}
}
if (!$user) {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => $scope,
'redirect_uri' => $app_url,
'display' => 'page',
));
print('<script> top.location.href=\'' . $loginUrl . '\'</script>');
}
Any assistance with this would be greatly appreciated.
You're doing this here:
if (!$user) {
If you don't want to redirect users who've already rejected the request, check for the presence of the error parameters Facebook uses to tell you the user has rejected the request, and don't send them back to the auth dialog if you see them.
At a very basic level, this should work and be easy to test provided your redirect_uri parameter in the login dialog brings the user back to the same page you're doing the user checking on:
if (!$user) {
if ($_REQUEST['error_reason'] == 'user_denied') {
//explain why you need them to log in
} else {
$loginUrl = $facebook->getLoginUrl(array( [...]
}
}

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.

Categories