Posting an image to fan page album using Facebook API - facebook

I have created an Facebook App. I need user to be able to post images to my facebook page's album through that App and for that I need to grab Access Token of the page. I can only get access token when I am logged in as an administrator of the page but if I log in with other account I don't get the access token of the page (Even though I liked the page and installed the App). Code is below
$fanpage = 'XXXXXXXXXXXXX';
$pages_arr = array(
'access_token'=>$access_token,
'fields'=>'access_token'
);
$page_token = $facebook->api('/'.$fanpage, 'get', $pages_arr);
$fanpage_token = $page_token['access_token'];
**//When logged in as admin of the page I get the token but I don't get the token if I am not //page admin**
echo $fanpage_token;
$file = "#".realpath("test.jpg");
$args = array(
'message' => 'test',
'image' => $file,
'aid' => $album_id,
'no_story' => 1,
'access_token' => $fanpage_token // note, we use the page token here
);
$photo = $facebook->api("/$album_id/photos", 'post', $args);
if( is_array( $photo ) && ! empty( $photo['id'] ) )
echo 'Photo uploaded. Check it on Graph API Explorer. ID: ' . $photo['id'];
I asking for following permissions on App install
manage_notifications,user_photos,photo_upload,publish_stream,friends_photos,manage_pages

I think this is intended behaviour. The manage_pages permission gives access to pages which the user administrates. If you need other (non admin) users to upload to your page, you'll need to store a valid access token for the page in your app. Then, use the saved access token to upload the image provided by the user.

Related

is it possible to post facebook page wall using a cron job?

Please don't vote and flag this quest. Please.
I am is the admin of the page. How can I post to that page wall using php SDK.
Is it possible or not?
the following code is posting as I post the page. I want the page itself post things to the wall.
<?php
include_once("inc/facebook.php"); //include facebook SDK
######### edit details ##########
$appId = '1157979879888106'; //Facebook App ID
$appSecret = '86403d4061433e23a0ouo9ec46a1405'; // Facebook App Secret
$return_url = 'http://xxxx.com/site/test'; //return url (url to script)
$homeurl = 'http://xxxx.com/site/index'; //return to home
$fbPermissions = 'publish_stream,manage_pages'; //Required facebook permissions
##################################
$page_id = "447126725331764";
//Call Facebook API
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret
));
//$fbuser = $facebook->getUser();
$publish = $facebook->api('/'.$page_id.'/feed', 'post',
array('access_token' => 'CAAQdLHAnOgoBAGAYZCIE09nfXaWZBXdMuwW5I379TjaWMZAQSUZCks9B4JfQyMXBEDN1CQZBaQJFeP44vWTGhic8AnkBgARge0AKJHIYgMdfDk16rq6ON496t0Phv6QtCmOR7t3wcybioNjxFkYctuN1ppDEBQdAObi6eIva0ZBTvAHJOWCSfcNicaVIZD',
'message'=> "Hello boss",
'from' => $appId,
'to' => $page_id,
'caption' => 'Caption',
'name' => 'Name',
'link' => 'http://www.example.com/',
'picture' => 'http://www.phpgang.com/wp-content/themes/PHPGang_v2/img/logo.png',
'description' => 'Hello boss'.' via demo.PHPGang.com'
));
?>
First of all, publish_stream is deprecated since many years. You really need to check out the Facebook docs, they state clearly that you need publish_pages and manage_pages to post "as Page".
That being said, that exact same question gets asked a LOT on Stackoverflow, so you can easily find a lot more information with the search function. In general, this is what you need to do:
Authorize the user with publish_pages and manage_pages
(Generate an Extended User Token)
Get a Page Token with /me/accounts
User that Page Token to post to the Page wall
More information about Access Tokens:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

Post to facebook page from facebook page tab app

I want to post activities from page tab app to my own facebook page. I referred many documents to post message to app.
My php code :
$GLOBALS["facebook"] = new \Facebook_Facebook(array('appId' => $facebook_app_id, 'secret' => $facebook_secret,));
$page_info = $GLOBALS["facebook"]->api("/pageid?fields=access_token");
print_r($page_info);die;
if (!empty($page_info['access_token'])) {
$args = array(
'access_token' => $page_info['access_token'],
'message' => 'TEST'
);
$postId = $facebook->api("/pageid/feed", "post", $args);
But pageinfo variable has only id. I'm not getting access_token. Any idea what else need to be done?
We need manage_pages permission which we can get it using https://www.facebook.com/dialog/oauth?client_id=client_app_id&client_secret=9ea3e2eff7c65b1fcf1a633da&redirect_uri=YOUR_REDIRECT_URL&scope=read_stream,publish_stream,offline_access,manage_pages.
After that using above code we will get access token which can be used to publish to our page

Facebook API post to friends wall

I'm building application to post statuses from authenticated users like that one.
The problem is the application posting from my profile on friends wall who authenticated the application from my profile.
I'm using Facebook API with CodeIgniter.
the code
$userId = $this->facebook->getUser();
$IDs = $this->facebook_data->get_IDs();
if(isset($_POST['link'])){
$args = array(
'from' => array('name'=>'Resala','id'=>'294357147348261'),
'application'=>array('name'=>'XXXX','id'=>'XXXXX'),
'name' =>'XXXX',
'message' => $_POST['status2'],
'link' => $_POST['link']
);
}else{
$args = array(
'from' => array('name'=>'Resala','id'=>'294357147348261'),
'application'=>array('name'=>'Resala','id'=>'XXXXX'),
'name' =>'XXXX',
'message' => $_POST['status1']
);
}
foreach($IDs->result() as $row){
$ID=$row->user_id;
$this->facebook->api("/$ID/feed", "post", $args);
}
The right docs for creating a post are:
http://developers.facebook.com/docs/reference/api/user/#posts
There you can see the params "from" and "application" don't exist and "name (can only be used if link is specified)".
It's not possible to post in the name of an app. You can only post in the name of an page (that may be assosiated with the app).
Therefor you have to use the page access token instead of the user access token.
See: http://developers.facebook.com/docs/reference/api/page/#page_access_tokens

200 Permissions error using graph api

after get my posts in my web page , then if user login he can add like / comment to that post problem is am getting an error if any user try add like or comment
200 Permissions error
if i login in with my username and password i can make like or add comment !!
am sending like via :
jQuery.post('https://graph.facebook.com/'+comm_id+'/likes/',{
access_token : "<?php echo $access_token ?>"
});
CODE :
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
if (session_id()) {
} else {
session_start();
}
$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', 'manage_pages');
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,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri' => 'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
header("Location: {$login_url}");
exit();
}
}
}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,manage_pages',
'fbconnect' => 1,
'display' => "page",
'redirect_uri'=>'http://localhost/fb/index.php',
);
$login_url = $facebook->getLoginUrl($login_url_params);
//redirect to the login URL on facebook
header("Location: {$login_url}");
exit();
}
$logoutUrl = $facebook->getLogoutUrl();
which access token is for the user is loged in
Make sure you have the publish_stream permission for any user you want to be able to create posts for. Remember, creating a comment is still creating content.
Checkout the publish_stream permission on Facebook's permissions documentation
Enables your app to post content, comments, and likes to a user's
stream and to the streams of the user's friends. This is a superset
publishing permission which also includes publish_actions. However,
please note that Facebook recommends a user-initiated sharing model.
Please read the Platform Policies to ensure you understand how to
properly use this permission. Note, you do not need to request the
publish_stream permission in order to use the Feed Dialog, the
Requests Dialog or the Send Dialog.

How do you post to the wall on a facebook page (not profile)

I have a blog site written in php and it posts new blog posts to twitter and a blog ping automatically under the hood using simple http post requests passed using php curl.
I have a facebook page for the blog site and want the updates to be posted to the wall on the page, is there a simple way to do this?
What I really want is a url and set of params to parcel up as an http post request.
Note that this is to post to the wall on a new style page not a profile.
Get PHP SDK from github and run the following code:
<?php
$attachment = array(
'message' => 'this is my message',
'name' => 'This is my demo Facebook application!',
'caption' => "Caption of the Post",
'link' => 'http://mylink.com',
'description' => 'this is a description',
'picture' => 'http://mysite.com/pic.gif',
'actions' => array(
array(
'name' => 'Get Search',
'link' => 'http://www.google.com'
)
)
);
$result = $facebook->api('/me/feed/', 'post', $attachment);
the above code will Post the message on to your wall... and if you want to post onto your friends or others wall then replace me with the Facebook User Id of that user..for further information look out the API Documentation.
This works for me:
try {
$statusUpdate = $facebook->api('/me/feed', 'post',
array('name'=>'My APP on Facebook','message'=> 'I am here working',
'privacy'=> array('value'=>'CUSTOM','friends'=>'SELF'),
'description'=>'testing my description',
'picture'=>'https://fbcdn-photos-a.akamaihd.net/mypicture.gif',
'caption'=>'apps.facebook.com/myapp','link'=>'http://apps.facebook.com/myapp'));
} catch (FacebookApiException $e) {
d($e);
}
Harish has the answer here - except you need to request manage_pages permission when authenticating and then using the page-id instead of me when posting....
$result = $facebook->api('page-id/feed/','post',$attachment);
You can not post to Facebook walls automatically without creating an application and using the templated feed publisher as Frank pointed out.
The only thing you can do is use the 'share' widgets that they provide, which require user interaction.
If your blog outputs an RSS feed you can use Facebook's "RSS Graffiti" application to post that feed to your wall in Facebook. There are other RSS Facebook apps as well; just search "Facebook for RSS apps"...
You can make api calls by choosing the HTTP method and setting optional parameters:
$facebook->api('/me/feed/', 'post', array(
'message' => 'I want to display this message on my wall'
));
Submit Post to Facebook Wall :
Include the fbConfig.php file to connect Facebook API and get the
access token.
Post message, name, link, description, and the picture will be submitted to Facebook wall.
Post submission status will be shown.
If FB access token ($accessToken) is not available, the Facebook Login
URL will be generated and the user would be redirected to the FB login
page.
Post to facebook wall php sdk
<?php
//Include FB config file
require_once 'fbConfig.php';
if(isset($accessToken)){
if(isset($_SESSION['facebook_access_token'])){
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}else{
// Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;
// Set default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
//FB post content
$message = 'Test message from CodexWorld.com website';
$title = 'Post From Website';
$link = 'http://www.codexworld.com/';
$description = 'CodexWorld is a programming blog.';
$picture = 'http://www.codexworld.com/wp-content/uploads/2015/12/www-codexworld-com-programming-blog.png';
$attachment = array(
'message' => $message,
'name' => $title,
'link' => $link,
'description' => $description,
'picture'=>$picture,
);
try{
//Post to Facebook
$fb->post('/me/feed', $attachment, $accessToken);
//Display post submission status
echo 'The post was submitted successfully to Facebook timeline.';
}catch(FacebookResponseException $e){
echo 'Graph returned an error: ' . $e->getMessage();
exit;
}catch(FacebookSDKException $e){
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
}else{
//Get FB login URL
$fbLoginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);
//Redirect to FB login
header("Location:".$fbLoginURL);
}
Refrences:
https://github.com/facebookarchive/facebook-php-sdk
https://developers.facebook.com/docs/pages/publishing/
https://developers.facebook.com/docs/php/gettingstarted
http://www.pontikis.net/blog/auto_post_on_facebook_with_php
https://www.codexworld.com/post-to-facebook-wall-from-website-php-sdk/