I know this error has been bitten to death over here, and I read every single one to have better understanding of my problem.
But my issue is a bit different, and I wonder if anyone can give me good suggestion where to look.
I'm using wordpress + wordpress social login. This plugin authenticates user and stores name/age/email/fbID/fbProfilePic in the DB.
I've a little feature on my website where users registered through facebook can click and post message to the their wall.
My code looks like this:
<?php
//IF user is registered via facebook, when he clicks INTERESTED message will appear on his/her wall to spread the news
$user = get_user_meta($current_user->ID,'Facebook',true);
if ($user && $_GET['commentstab'] == 1 && !$_POST['delmycom']) {
require ('Facebook/facebook.php');
//Access details
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXX'
//'cookie' => true
));
//try {
$params = array(
'message' => "Hurray! This works :)",
'name' => "This is my title",
'caption' => "My Caption",
'description' => "Some Description...",
'link' => "http://stackoverflow.com",
'picture' => "http://i.imgur.com/VUBz8.png",
);
$post = $facebook->api("/$user/feed","POST",$params);
echo "Your post was successfully posted to UID: $user";
//} //try
//catch (FacebookApiException $e) {
// $result = $e->getResult();
//} //catch
} //master if
?>
I read in various topics that I need publish_stream permission from user to perform this action. But since I store user info separately in my own DB, how do I get this publish stream permission? Do I need to modify wordpress plugin to store some kind of access token? and utilize this token to post to wall?
you generate loginurl with publish_stream permission
if user is not logged in with permission, you have to make sure he does
redirect user to proper page
you can do it like this,
<?php
include_once("facebook.php");
$facebook = new Facebook(array(
'appId' => APP_ID,
'secret' => APP_SECRET,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
try {
// Get the user profile data you have permission to view
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' =>'publish_stream','redirect_uri'=>'example.com'));
die('<script> top.location.href="'.$loginUrl.'";</script>');
}
$params = array(
'message' => "Hurray! This works :)",
'name' => "This is my title",
'caption' => "My Caption",
'description' => "Some Description...",
'link' => "http://stackoverflow.com",
'picture' => "http://i.imgur.com/VUBz8.png",
);
$post = $facebook->api("/$user/feed","POST",$params);
?>
Related
I'm using FB App to publish posts to my FB pages.
So when i use this code for links, it works.
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true
));
$attachment = array(
'access_token' => $row["token"],
'name' => $_POST["name"],
'picture' => $_POST["picture"],
'message' => $_POST["message"],
'link' => $_POST["link"]);
try
{
$facebook->api("/".$row['id']."/feed", "POST", $attachment);
}
catch(Exception $e)
{
echo('exception caught '.$e->getMessage());
}
But, when i want to share pictures as a page:
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true,
'fileUpload' => true,
'allowSignedRequest' => false
));
$attachment = array('access_token' => $row["token"], 'url' => 'http://image.link.jpg', 'message' => $_POST["message"]);
try
{
$facebook->api("/".$row['id']."/photos", "POST", $attachment);
}
catch(Exception $e)
{
echo('exception caught '.$e->getMessage());
}
The app post picture as user. USER to PAGE.
So i found that i need to use publish_pages permission, but my access token contains both publish_actions and publish_pages. I can't change that because I'm using one access token for a lot of things.
So i have to ask, is there a way to explicitly choose what permissions i want to use from access token. FB API have same code for page and user picture publish /user_id/photos and /page_id/photos.
Need to use Page access token, there's no other way.
I'm using https://github.com/pocesar/facebook-kohana for facebook login. I have problem with facebook logout. It doesn't destroy facebook session. I've tried so many things and I've read many questions. I tried this in my logout method, but no result:
$this->redirect('https://www.facebook.com/logout.php?
next=mysite.dev
&access_token=USER_ACCESS_TOKEN');
My logout method is:
public function action_logout(){
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'mySecret',
));
$user = $facebook->getUser();
$facebook->destroySession();
Session::instance()->delete('user');
$this->redirect('/');
}
How to destroy session, so that user can log in my site with another facebook account? Thanks!
My method login is:
public function action_fbLogin(){
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'Secret',
));
$user = $facebook->getUser();
if ($user) {
$user_profile = $facebook->api('/me', array('fields' => 'id,email,name,first_name,last_name,picture'));
$user_id = Model_UserFunctions::checkIfUserExist($user_profile['email']);
if($user_id > 0)
{
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
//var_dump($_SESSION);
$this->redirect('profile');
exit;
}
$values = array(
'email' => $user_profile['email'],
'username' => $user_profile['email'],
'password' => '12345678'
);
$user = ORM::factory ( 'User' );
$user->values($values);
try
{
if($user->save()){
$user_new_id = $user->as_array();
$user_new_id = $user_new_id['id'];
Session::instance()->set('user', array(
'fb_id' => $user_profile['id'],
'user_id' => $user_new_id,
'pic' => $user_profile['picture'],
'email' => $user_profile['email'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name'],
));
$this->redirect('profile');
}
}
catch (ORM_Validation_Exception $e)
{
$result = $e->errors('models');
echo '<pre>';
print_r($result);
exit;
}
}
else
{
$this->redirect($facebook->getLoginUrl(array('id,email,name,first_name,last_name,picture')));
}
exit;
}
Edited: My goal to use Facebook logout is because in my site I only use Facebook login, there isn't another way to log in my site, that's idea of it. And I should have logout method in my site, so when user wants to logout, he could do it.
This logouts me from facebook but facebook login page is shown. How to redirect next to my site. I've set it as next but it doesn't redirect to it:
<a href="https://www.facebook.com/logout.php?
next=http://mysite.dev
&access_token=Null">Logout</a>
You can get facebook logout url using facebook-php-sdk API.
When user will click on that url he will be offline from his facebook account.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
For more info you can check this url.
https://github.com/facebookarchive/facebook-php-sdk
Then you don't have a problem with facebook
Is only the session that needs to be destroyed...
On this page of PHP manual there is an example showing you might need aditional steps to destroy a session beyond unset($_SESSION)
http://php.net/manual/en/function.session-destroy.php
Forcing the users to log off from Facebook does not seem a good move for me.
Millions of FB users have intentionally asked to stay logged in and if they visit your site and logoff from there they might think it wasn't very friendly of you to log them off the FB accounts. many don't even remember their pwd... so you would be creating a problem for them and they might never comeback to your site... Just saying
I am trying to publish a checkin using facebook api. Sometimes, it works well and posts the checkin, but mostly 99% of the times it produces the error: "This webpage has a redirect loop"
checkin.php:
<?php
require("../src/facebook.php");
// construct the object with your facebook app data
$facebook = new Facebook(array(
'appId' => 'XXXXX',
'secret' => 'XXXX',
'cookie' => false
));
$token = $facebook->getAccessToken();
//echo $token;exit())
try {
// to get the id of the currently logged in user
// if, you want you can manually set a user id here like this:
//$uid = '[FB USER ID]';
$uid = $facebook->getUser();
$facebook->setAccessToken($token);
$facebook->api('/'.$uid.'/checkins', 'POST', array(
'access_token' => $facebook->getAccessToken(),
'place' => '101697613295949',
'message' => 'Enjoying Chill Beer with Team',
'picture' => 'http://test.com/someplace.png',
'coordinates' => json_encode(array(
'latitude' => '28.541203543000023',
'longitude' => '77.15503053709995',
'tags' => 'XXXX'))
));
echo 'You are checked in';
} catch (Exception $e){
// No user found - ask the person to login
$login_url = $facebook->getLoginUrl();
header("Location: ".$login_url);
}
?>
Thanks in advance.
I recently created a Facebook App to send visualized data (image) to pages which are already belongs to same user account as The App.
Also, I set extended permissions in Permissions Pane of App. Setting Page.
Here are the codes that can send post in text format. But when I tried to send images attached it fails.
What are the possible mistakes that I can't get at the moment?
$facebook = new Facebook(array(
'appId' => MY_APP_ID,
'secret' => MY_APP_SECRET,
'fileUpload' => true,
'allowSignedRequest' => false
));
$img = "../../../tw-data/postImages/twpostimage-usd.png";
$message = 'Activity Test with Image # ' . date('d.M.Y H:i:s');
try
{
$access_token = $facebook->getAccessToken();
$params = array('access_token' => $access_token,
'source' => '#'.$img,
'message' => $message
);
$result = $facebook->api('/'. MY_PAGE_ID .'/feed','POST', $params);
print_r($result);
}
catch (FacebookApiException $e)
{
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
What Errors are you getting when you try this?
I believe the main issue you're having is that you're posting to
/my_page_id/feed
where you should be posting to
/my_page_id/photos
From the facebook docs
$response = $facebook->api(
"/me/photos",
"POST",
array (
'url' => '{image-url}',
)
);
EDIT:
I have successfully posted an image with a message using the following format.
$img = 'FILE_NAME.jpg';
$message = 'Activity Test with Image # ' . date('d.M.Y H:i:s');
$params = array('access_token' => $access_token,
'message' => 'test',
'source' => '#'.$img
);
$result = $facebook->api('/USER_ID/photos','POST', $params);
print_r($result);
Also, I set extended permissions in Permissions Pane of App. Setting
Page.
That's a common mistake. Permissions have to be requested in your scope, not in the app settings.
$params = array(
'scope' => 'publish_stream',
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
Accept the permissions dialog and try again. If you want to post to the page as page, obtain a page access token.
Happy Coding!
I am trying to schedule wall posts to be added to the Facebook business page in the future.
As far as I can see Facebook does not recommend to use "offline_access" anymore.
How would you do that?
This is my code so far. It works if I am already logged into Facebook.
EDIT: Naturally I will create some code that check the schedule that I pull from the database. And use a cron job to regulary check that schedule.
require_once('src/facebook.php');
$config = array(
'appId' => 'xxxxxxx',
'secret' => 'xxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$page_info = $facebook->api("/PAGE_ID?fields=access_token");
try {
$ret_obj = $facebook->api('/PAGE_ID/feed', 'POST',
array (
'link' => 'http://www.example.com/',
'message' => 'This is a test',
'access_token' => $page_info['access_token']
));
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
print_r($e->getType());
print_r($e->getMessage());
}
You have to extend access_token periodically now. All other code should be working fine as previously