How do I give a Facebook App permission to post images to a Facebook Page album?
This is the code I use:
$args = array(
'image' => '#' . $img,
'aid' => $album_id,
'no_story' => 1,
'access_token' => $app_access_token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
This is the error I get:
Uncaught OAuthException: A user access token is required to request this resource.
Here is the code I use to publish a photo to an album on Facebook. I hope it'll help.
<?php
include '../src/facebook.php';
$config['appId']='app_id';
$config['secret']='API_KEY';
$config['fileUpload'] = true;
$param['redirect_uri']='your_redirect_url';
$param['scope']=',publish_stream,user_photos';
$facebook=new Facebook($config);
$userid=$facebook->getUser();
if($userid){
try{
$args = array(
'image' => '#' . $img,
'message'=>$message,
);
$photo = $facebook->api('/'.$album_id . '/photos', 'post', $args);
}
catch(FacebookApiException $e){
echo $e->getMessage();//Get OAuth Error
}
else{
$loginUrl=$facebook->getLoginUrl($param);
echo "Please <a href='$loginUrl'>Click Here</a> To Login";
}
?>
Learn more Here
Just in case any user stumbles upon this post , to resolve this error
1> $facebook->setaccesstoken() to facebook page's access token . The facebook page access token is available by issuing $facebook->api('/user_id/accounts'), here user id is the id of page's admin .
2> After setting access token you can use the facebook->api to post the photo to album using album id
$facebook->api('album_id/photos', 'post', array( 'source' => '#'.$photo ));
Related
I have a android app. User can access to the app using facebook login. Once user logged in, his all data/activity will push to web server using php.
I just wanted to validate user access for app while he accessing the app.
Just wanted to check user app access status using his access token when his request comes to php web server.
The main aim is to deny fake users request from android to web server
for the security purpose.
Can anyone help me to sort out my problem .
Thanks in advance..
<?php
try{
require_once(dirname(__FILE__) . '\facebook-php-sdk\src\Facebook\facebook.php' );
}
catch(Exception $o){
print_r($o);
}
$config = array(
'appId' => '123456',
'secret' => '5ca2b11ea4c8sdsdsd',
'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('/'.$user_id.'/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
// $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.
echo "<pre>";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.';
}?>
I am trying to post a status update on the wall of a group which I am a member of. Here is the code I am using
<?php
require 'facebook-php-sdk/src/facebook.php';
$appId = 'xxxxxxxxxxxxxxxx';
$appSecret = 'xxxxxxxxxxxxxxxx';
$extended_access_token = 'xxxxxxxxxxxxxxxxxxxxx';
$facebook = new Facebook(array('appId' => $appId, 'secret' => $appSecret));
$msg_body = array(
'message' => 'Good evening',
'type' => 'status',
'access_token' => $extended_access_token,
);
$groups = array(
'Group name' => '1234567',
);
foreach($groups as $group_name => $group_id){
try {
$post_url = "/$id/feed";
$postResult = $facebook->api($post_url, 'post', $msg_body );
print_r($postResult);
} catch (FacebookApiException $e) {
echo $e;
}
}
?>
If I login to fb via browser and hit this page in a new tab, the message is getting posted to the group wall. But If I am not logged into facebook, then if I hit this page, no message is getting posted and I am getting an error message
OAuthException: (#200) The user hasn't authorized the application to perform this action
How can I post to this group via offline mode? Searched a lot for this, could not find any useful info.
you need to have the permissions from the group owner
Change $id to $group_id and you'll be fine.
you ned to get apps this permession : publish_actions,publish_stream,user_groups,
On my page I want post feeds with an image in the text of message and not like a link/url, is possible add to text message something like htmltags or bbcode?
$msg = "<img src=\"urlimg or facebook\" >\nMy text here";
$args = array(
'message' => $mgs,
);
$myfeed = $facebook->api($pageid . '/feed', 'post', $args);
update:
i've found a solution but if i post 2 times in a row they will be groupped in the same box in timeline
$args = array(
'message' => $msg,
'image' => '#'.$path,
'aid' => $album_id,
'access_token' => $token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
exist a setting to stop auto-group that? or there is another way to post it like feed with image?
You can't publish an image in the middle of the text message, Facebook do not allow it.
But you can attach an image to the message, it will appear on the left of the message in this way:
$msg = "My text here";
imgUrl = "http://urltotheimage.com/path/image.jpg";
$args = array(
'message' => $mgs,
'picture' => $imgUrl
);
$myfeed = $facebook->api($pageid . '/feed', 'post', $args);
So I spent 30 seconds searching around the PHP Facebook API, which really is what you should be doing, and found the following example:
<?
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = './mypic.png'; // Path to the photo on the local filesystem
$message = 'Photo upload via the PHP SDK!';
?>
<html>
<head></head>
<body>
<?
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 {
// Upload to a user's profile. The photo will be in the
// first album in the profile. You can also upload to
// a specific album by using /ALBUM_ID as the path
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,
)
);
echo '<pre>Photo ID: ' . $ret_obj['id'] . '</pre>';
} 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.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'photo_upload'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
echo '<br />logout';
} else {
// No user, print a link for the user to login
// To upload a photo to a user's wall, we need photo_upload 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' => 'photo_upload') );
echo 'Please login.';
}
?>
</body>
</html>
Take note of the $config variable values and the $facebook->api() call.
I finally got facebooks graph api to post messages on my fan PAGE as page
How do i get it to post large images as a post, not as a link?
'source' => $photo seems to create a thumbnail
this is what i have so far
<?php
$page_id = 'YOUR-PAGE-ID';
$message = "I'm a Page!";
$photo = "http://www.urlToMyImage.com/pic.jpg";
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'YOUR-APP-ID',
'secret' => 'YOUR-SECRET-ID',
));
$user = $facebook->getUser();
if ($user) {
try {
$page_info = $facebook->api("/$page_id/?fields=access_token");
if( !empty($page_info['access_token']) ) {
$facebook->setFileUploadSupport(true); // very important
$args = array(
'access_token' => $page_info['access_token'],
'message' => $message,
'source' => $photo
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => 'http://mydomain.com/logout_page.php' ));
} else {
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
}
?>
The problem here is that you are in actual fact not posting a photo. What you are doing is posting a link to that photo so what you see is indeed a thumbnail preview image that Facebook retrieved from that URL.
What you'll want to do is provide a full path to a file on your server prefixed with the # symbol. The topic has been discussed on the site quite a bit so I'll just point you in the direction of a canonical post dealing with uploading of images to Facebook with the PHP SDK
Upload Photo To Album with Facebook's Graph API
The code looks like this -
$facebook->setFileUploadSupport(true);
$params = array('message' => 'Photo Message');
$params['image'] = '#' . realpath($FILE_PATH);
$data = $facebook->api('/me/photos', 'post', $params);
I am developing an app to include in my page tab. In this app, the user will go take a picture with your webcam and the picture will sent to an especific album in my fan page.
I found an script here: and he is working very well, but I've a problem, the script use an user access_token to upload the picture, dont's necessary ask permission of all users to upload the picture because the script use my administrator user to send to the page.
The big problem is: The user access token expire after 2 hours or when my admin user not logged in, the permission offline_access was discontinued and I don't know how my script will work.
I need that all user can upload photo using the system, anyone know how I can work?
Here is the script in PHP:
`
require_once 'library/facebook.php';
$facebook = new Facebook(array(
'appId' => '<appid>',
'secret' => '<appsecret>',
'fileUpload' => true
));
$access_token = 'access_token';
$params = array('access_token' => $access_token);
$fanpage = 'page_id';
$album_id ='album_id';
$accounts = $facebook->api('/ADMIN_ACCOUNT/accounts', 'GET', $params);
foreach($accounts['data'] as $account) {
if( $account['id'] == $fanpage || $account['name'] == $fanpage ){
$fanpage_token = $account['access_token'];
}
}
$valid_files = array('image/jpeg', 'image/png', 'image/gif');
$img = realpath("image_path");
$args = array(
'message' => 'message to write in legend',
'image' => '#' . $img,
'aid' => $album_id,
'no_story' => 1,
'access_token' => $fanpage_token
);
$photo = $facebook->api($album_id . '/photos', 'post', $args);
if( is_array( $photo ) && !empty( $photo['id'] ) ){
echo '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to watch this photo on Facebook.</a></p>';
}`
You will need to request a long-lived access token by hitting the endpoint:
https://graph.facebook.com/oauth/access_token?
client_id=APP_ID&
client_secret=APP_SECRET&
grant_type=fb_exchange_token&
fb_exchange_token=EXISTING_ACCESS_TOKEN
Take a look at Scenario 4 on the following document:
http://developers.facebook.com/roadmap/offline-access-removal/
If you’re using that script to post to a fan page’s album, then you should get a page access token – they don’t expire.
Details see here: https://developers.facebook.com/docs/authentication/pages/