Posting a comment on the news feed - php

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.

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

Login with facebook suddenly stopped working

Suddenly login with facebook in my web application stopped working.
The following is code of my login page for website. i am using facebook php sdk which was working fine till 5 hours back now it stopped.
I am confused and want to know what the hell happen to it as i am not able to solve it since last 5 hours. I have removed my php code connecting to database and query code to make it look simple.
I take only two scopes that is email and publish_stream
output of below code is hello your fb user id is
<?php
require_once 'include/data.php';
//check to see if they're logged in
if(isset($_SESSION['logged_in'])) {
header("Location: index.php");
}
$site_url = "http://example.com/facebook.php";
require_once ('phpsdk/src/facebook.php');
// Create our application instance
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxx',
'allowSignedRequest' => false
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl(array(
'redirect_uri' => 'http://example.com/logout.php',
));
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email, publish_stream',
'redirect_uri' => $site_url,
));
}
// checking
if(!$user) {
//echo "<a href='$loginUrl' >Login</a>";
header("Location:$loginUrl");
} else {
// echo "<a href='$logoutUrl' >Logout</a><br />";
$f_name = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $user_info['first_name']);
$l_name = mysqli_real_escape_string($GLOBALS["___mysqli_ston"], $user_info['last_name']);
$fb_email = $user_info['email'];
$fb_uid = $user_info['id'];
echo "hello your fb user id is $fb_uid"; // just sample to check
}
?>
The lifetime of the FB token is valid for just 2 hours. From the FB Docs...
When retrieving a Facebook access token, an expiration time associated
with the access token is also typically returned. If this expiration
time is exceeded, your application will need to obtain a new access
token (which will also have a new expiration time associated with it).
However you can exchange the token and get a new one that has an extended validity say 60 days.. Something like this..
$this->getUrl('graph', '/oauth/access_token'), array(
'client_id' => $this->getAppId(),
'client_secret' => $this->getAppSecret(),
'grant_type'=>'fb_exchange_token',
'fb_exchange_token'=>$this->getAccessToken()
)
More information regarding this can be found here

facebook login returns code as variable in query string

I am using a Facebook login in one of my website using the sdk for PHP.
Sometimes the facebook login works and sometimes it doesn't.
Here is my index.php:
/// fb login area.....
include("facebook/src/facebook.php");
$facebook = new Facebook(array(
'appId' => '159326560901256',
'secret' => 'c6b30bd2f80747b64d5b1d15a3da9e9c',
));
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => 'http://xyz.com/login_fb.php'
));
<a href="<?=$loginUrl?>"><img class="fb_img"
src="images/fb_logo.png" width="153" height="38" /></a>
it goes to login_fb.php:
https://www.facebook.com/dialog/oauth?client_id=123131231&redirect_uri=http%3A%2F%2Fxyz.com%2Flogin_fb.php&state=403bc8aaf9eaee4064e3cc27befc71ff&scope=email
now on login_fb.php i get this url:
login_fb.php?code=ASA&S^&*A^S&*A^S&*^A&*ASUAUIYSUIYASIYAUSYUIASA
and in $user I get 0
/// code for login_fb.php
include("facebook/src/facebook.php");
$facebook = new Facebook(array(
'appId' => '159326560901256',
'secret' => 'c6b30bd2f80747b64d5b1d15a3da9e9c',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
$logoutUrl = $facebook->getLogoutUrl(array(
'next' => 'http://minuteville.com/index.php?val=fblogout'
));
Why isn't it working?
By adding the following line and using getAccessTokenFromCode in Facebook SDK,
the problem can be resolved.
The acesss code expires. So getting new access code by exchanging code resolves the problem
$access_code = $facebook->getAccessTokenFromCode($_GET['code']);
https://developers.facebook.com/docs/howtos/login/server-side-login/#step6
You should be able to build the access token with the code parameter to get the user. It's all written in the doc.

How do I make a post using Facebook's v3 SDK and PHP?

I am trying to make a post on the user's Facebook wall using my application. I also want to be able to access and display the person's user information afterward - but I'm getting stuck.
The code I have so far is below (with the sensitive parts masked, of course):
<?php
include("src/facebook.php");
$facebook = new Facebook(array(
'appId' => ##############,
'secret' => #####################################,
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
// The access token we have is not valid
$user = null;
}
}
if (!$user) {
$args['scope'] = 'offline_access, read_stream, friends_likes, email, read_stream, publish_stream, user_birthday,user_hometown, user_photos, uid, username, first_name';
$loginUrl = $facebook->getLoginUrl($args);
}
if (!$user): ?>
Login with Facebook
<?php endif ?>
I can't figure out what's going wrong - and maybe I'm missing something critical? Any guidance would be appreciated.
1) make sure to read the permissions documentation. For instance, offline_access has been deprecated and most of the permissions you are asking for does not exists!
2) to post on the user wall you need the publish_stream permission (which you are already requesting)
3) follow my tutorial, something like the below will get you started:
$args = array(
'message' => 'Hello from my App!',
'link' => 'http://www.masteringapi.com/',
'caption' => 'Visit MasteringAPI.com For Facebook API Tutorials!'
);
$post_id = $facebook->api("/me/feed", "post", $args);
4) improve your view (mixing PHP and HTML in your above code is not programmatically wrong but not recommended). And to answer your last request, the $user_profile would hold the information you need, just check the Facebook example to get started.
5) try to improve your English and welcome to Stack Overflow!

How to request scope/permission from Facebook during user registration

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

Categories