Facebook app error or my mistake strange thing happened to me - facebook

I created a facebook application which create as new image and upload it to users albums but when they are using the app that image is uploading to my albums. that sounds funny but it is shocking to me how can their images can upload to me. please check my code if i made any mistakes.
$access_token = 'ABCDEFGHIJKLMN';
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$album_name = 'Klu report';
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => 'sai ram'
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
$album_id = $result->id;
$graph_url= "https://graph.facebook.com/"
. $album_id ."/photos?"
. "url=" . urlencode('https://yourkishore.com/facebook/'.$output)
. "&message=" . urlencode('I got my Placements report get yours here : https://apps.facebook.com/yourkishore')
. "&method=POST"
. "&access_token=" .$access_token;
echo '<html><body>';
file_get_contents($graph_url);
echo '</body></html>';

Which user Facebook API believes this photo should go to is directly tied to the access_token. Start debugging there. Put a few access_tokens from different users in the debugger here and see what you get.

Related

Facebook notifications - need permission or not?

Here is what I'm seeing in the docs:
Apps can send notifications to any existing user that has authorized the app. No special or extended permission is required.
Okay, sounds good. I'm using the JS SDK and here is what I'm trying to do:
FB.api('/me/notifications?access_token=' + window.accessToken + '&href=test&template=test', function(response) {
console.log(response);
});
This is what I'm getting:
"(#200) The "manage_notifications" permission is required in order to query the user's notifications."
I have tried replacing the href parameter with my app's real domain. Using my facebook ID instead of "/me/" makes no difference either. HELP!
I HAVE tried adding the manage_notifications permission (and still doesn't work...), but my question is why does it say the opposite in the docs?
EDIT: Went to PHP:
<?php
include_once('sdk/facebook.php');
$user = $_POST['user'];
$message = $_POST['message'];
$config = array();
$config['appId'] = '609802022365238';
$config['secret'] = '71afdf0dcbb5f00739cfaf4aff4301e7';
$facebook = new Facebook($config);
$facebook->setAccessToken($config['appId'].'|'.$config['secret']);
$href = 'href';
$params = array(
'href' => $href,
'template' => $message,
);
$facebook->api('/' . $user . '/notifications/', 'POST', $params);
?>
EDIT 2: After a silly logic mistake it now works :)
To send a notification you must use application access token - appid|appsecret, so you should send it server side and execute via AJAX call. PHP example:
require_once("facebook.php");
$config = array();
$config['appId'] = 'YOUR_APP_ID';
$config['secret'] = 'YOUR_APP_SECRET';
$facebook = new Facebook($config);
$facebook->setAccessToken($config['appId'].'|'.$config['secret']);
$user = 'userid';
$message = 'message';
$href = 'href';
$params = array(
'href' => $href,
'template' => $message,
);
$facebook->api('/' . $user . '/notifications/', 'post', $params);
https://developers.facebook.com/docs/concepts/notifications/

Facebook. After approve permission wrong redirect link

so I have a little problem, when first time user use my app It is redirected to:
http://www.facebook.com/MY_APP.../?state=f5c913239670dc934fae1274a81fa538&code=AQAmY5k8gRRt7woNKmpc_6z2eHgT-2B3prjNEDYFma1KqpQDKob-CS_bWfqd0qPkQD-tGoVdKUK8twGZRnLhoiotpZ3SlBeo3uusfmMyWDYStv-D79RvQEP8m9M8S8GYlbN3x1eLEd4D3RajWokTNAhRP8a1VB9VSKAb35xXFzg3PkheRKHqjr-nex5jcjM7Nv2B_JEV1w8AzM2LJg2M9kAb#_=_
And I get error: page not found
Durring permission approvig my link is: https://www.facebook.com/dialog/oauth?client_id=456080124457246&redirect_uri=http%3A%2F%2Fpadekime.wu.lt%2Fplaukai%2Findex.php&state=2c46a86dae43af0467da756bc3e52779&scope=publish_stream%2Cphoto_upload
I added redirect link and now It go to:
http://www.padekime.wu.lt/plaukai/kontekstas/?state=5abfd521df3c68ae55f26ad51a704743&code=AQDMmpZC-_Yxatq4xuAE60Tl2qW4pXCvDeghrbMGGu3Lg1X8zqrk-lvxRc3VOoYvgoLvATJhcsyykZnPoM7XI2sXih-nJhKrjx-HS3GFapELmC4KvX4KyN-VU-znHopkA-q_zcuIffJ0tY79CXPn7mo05BpxfvPntXaUAe7ymLf1p8Kg29eERaP5nw1dChbwwZ13FNY7BXD2ymAsFHPNH5zw#_=_
Possible to make that after permission approved go normally to my app http://www.facebook.com/Padekime/app_456080124457246 without /?state=..... ?
My code now looks like:
<?php
require_once('images/Facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'e2956a6e1de8791363faedb1ef44c408',
));
# Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$redirectUri = 'http://www.padekime.wu.lt/plaukai/kontekstas/';
# Photo Caption
$photoCaption = 'Patarimų plaukams sužinojau čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl,
'redirect_uri' => $redirectUri
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
$user = null;
error_log($e);
}
} else {
$redirectUri = 'http://www.padekime.wu.lt/plaukai/kontekstas/';
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload',
'redirect_uri' => $redirectUri
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
As I understand problem is somewhere here, but I cant solve it.
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
So if you know something about this, please help me, thank you very much.
SOLUTION:
I added ?ref=ts to $redirectUri:
$redirectUri = 'http://www.facebook.com/Padekime/app_456080124457246?ref=ts';
SOLUTION:
I added ?ref=ts to $redirectUri:
$redirectUri = 'http://www.facebook.com/Padekime/app_456080124457246?ref=ts';

Problems by uploading Photo to album by Facebook API - PHP

I have problems to upload a photo to an album by the facebook API. this is my code.
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'Message',
'name'=> 'Album Name'
);
$create_album = $facebook->api('/me/albums?access_token='.$access_token, 'post', $album_details);
//Get album ID of the album you've just created
$album_id = $create_album['id'];
echo $album_id." - ";
//Upload a photo to album of ID...
$photo_details = array();
$img = "app.jpg";
$photo_details['source'] = '#' . $img;
$photo_details['message'] = 'Wow.. cool image!';
$upload_photo = $facebook->api('/'.$album_id.'/photos?access_token='.$access_token, 'post', $photo_details);
When i upload the image with a form, it works! but this code does not upload the image into the album.
I have tried also with CURL but there is nothing... i don't know where the problem is...
After testing few things on Graph API Explorer, Here's a working PHP Version:
<?php
# Path to facebook's PHP SDK.
require_once("facebook.php");
# Facebook application config.
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'fileUpload' => true # Optional and can be set later as well (Using setFileUploadSupport() method).
);
# Create a new facebook object.
$facebook = new Facebook($config);
# Current user ID.
$user_id = $facebook->getUser();
# Check to see if we have user ID.
if($user_id) {
# If we have a user ID, it probably means we have a logged in user.
# If not, we'll get an exception, which we handle below.
try {
# Get the current user access token:
$access_token = $facebook->getAccessToken();
# Create an album:
$album_details = array(
'access_token' => $access_token,
'name' => 'Album Name',
'message' => 'Your album message goes here',
);
$create_album = $facebook->api('/me/albums', 'POST', $album_details);
# Get album ID of the album you've just created:
$album_id = $create_album['id'];
# Output album ID:
echo 'Album ID: ' . $album_id;
# Upload photo to the album we've created above:
$image_absolute_url = 'http://domain.com/image.jpg';
$photo_details = array();
$photo_details['access_token'] = $access_token;
$photo_details['url'] = $image_absolute_url; # Use this to upload image using an Absolute URL.
$photo_details['message'] = 'Your picture message/caption goes here';
//$image_relative_url = 'my_image.jpg';
//$photo_details['source'] = '#' . realpath($image_relative_url); # Use this to upload image from using a Relative URL. (Currently commented out).
$upload_photo = $facebook->api('/' . $album_id . '/photos', 'POST', $photo_details);
# Output photo ID:
echo '<br>Photo ID: ' . $upload_photo['id'];
} 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' => 'publish_stream, user_photos'));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
# No user, print a link for the user to login and give the required permissions to perform tasks.
$params = array(
'scope' => 'publish_stream, user_photos', # These permissions are required in order to upload image to user's profile.
);
$login_url = $facebook->getLoginUrl($params);
echo 'Please login.';
}
?>
I have added comments so you could understand what it does by reading the code.
This works with both absolute url and relative url, I have commented out code for uploading image using relative url as you have mentioned in your comments you can't read real path of the image.
EDIT: Note: The user has to give extended permissions to your facebook application to upload images to their profile, Those permissions are publish_stream and user_photos.
Let me know if this helped you and if it works :)
$user = $this->facebook->getUser();
$this->facebook->setFileUploadSupport(true);
$user_profile = $this->facebook->api('/me');
$album_details = array(
'message' => 'Hello everybody this is me ' . $user_profile['name'],
'name' => 'I am so slim because I dont have money to eat....:('
);
$create_album = $this->facebook->api('/me/albums', 'post', $album_details);
// Upload a picture
$photo_details = array(
'message' => 'I am so slim because I dont have money to eat....:('
);
$photo_details['image'] = '#' . realpath('./a.jpg');
$upload_photo = $this->facebook->api('/' . $create_album['id'] . '/photos', 'post', $photo_details);
Please use $facebook on $this->facebook

uploading photos to facebook album from a url sent via http post from another website

im working on a website that lets users view photos.. i wanted them to be able to press on the photo and the photo gets sent staight to their facebook account... so far ive found this documentation that allows users to upload photos from local mashine How-To: Use the Graph API to Upload Photos to a user’s profile.. is there an way of modifying this code so it recieves a url from a http post from another website?
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_POST-LOGIN_URL";
$album_name = 'YOUR_ALBUM_NAME';
$album_description = 'YOUR_ALBUM_DESCRIPTION';
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code))
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=>
'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false,
$context));
// Get the new album ID
$album_id = $result->id;
//Show photo upload form and post to the Graph URL
$graph_url = "https://graph.facebook.com/". $album_id
. "/photos?access_token=" . $access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url. ' "method="POST">';
echo 'Adding photo to album: ' . $album_name .'<br/><br/>';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message" type="text"
value=""><br/><br/>';
echo '<input type="submit" value="Upload" /><br/>';
echo '</form>';
echo '</body></html>';
}
?>
https://developers.facebook.com/docs/reference/api/photo/:
You can also publish a photo by providing a url param with the photo's URL.
That’s all you have to do – post to /PROFILE_ID/photos, and replace the source parameter with one called url, containing the address of the photo where it is publicly reachable via HTTP.

how to post on friends' walls using php sdk?

i am trying to post to my friends' feeds using this code, but it is not working . i am stuck, any help ??
$app_url ="http://localhost.local/PMS/facebook/PostWithPHP.php";
$facebook = new Facebook(array(
'appId' => 'APPID',
'secret' => 'APPSECRET',
'cookie' => true,
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
$user_friends = $facebook->api('/me/friends');
sort($user_friends['data']);
try {
// Proceed knowing you have a logged in user who's authenticated.
$access_token = $facebook->getAccessToken();
$vars = array(
'message' => 'My Message',
'name' => 'title',
'caption' => 'Caption',
'link' => 'Link',
'description' => 'Description',
'picture' => 'image'
);
foreach($user_friends['data'] as $f){
$sendTo = $f['id'];
$sendToName = $f['name'];
$result = $facebook->api("/".$sendTo ."/feed", 'post', $vars);
}
} 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();
} else {
$loginUrl = $facebook->getLoginUrl(array('redirect_uri'=> $app_url));
echo "<script type='text/javascript'>";
echo "top.location.href = '{$loginUrl}';";
echo "</script>";
}
and another question is that using this code, but with replacing $facebook->api("/".$sendTo ."/feed", 'post', $vars); by $facebook->api("/me/feed", 'post', $vars); and of course without looping friends, posts on my timeline. how can i make it post on my wall ??
I guess for a post to a timeline you will need an accessToken from the user where to publish the content. In your case you just have the accessToken of the registered user, not of his friends. That is a restriction by FB I think.
1st check you getting the user id(place echo and check) if not try this... I think this will help you brother
$token_url = "https://graph.facebook.com/oauth/access_token?" ."client_id=" . $app_id ."&client_secret=" . $app_secret .
"&grant_type=client_credentials";
$access_token = file_get_contents($token_url);
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
$user_id = $data["user_id"];
There are a few things wrong with your code. First of all, make sure the link and picture parameters for the post are valid URLs. Facebook will give you a error message otherwise ((#100) link URL is not properly formatted). Also, the link must go to the Canvas or Site URL for your application.
That should solve the issues with posting to a friend's wall.
However, may I remind you that your application is in violation of the Facebook Platform Policy. Facebook doesn't allow multiple posts to the stream (whether its yours or a friends) unless there is explicit permission from the user. It also to stop common friends seeing the same message from multiple friends.
You can follow the tutorial here:
https://www.webniraj.com/2012/11/22/facebook-api-posting-a-status-update/
But, instead of making a API call to: /me/feed, you replace me with the friend's User ID, so it looks like /12345/feed
Please note that Facebook has now disabled posting to Friends' walls via the API. Instead, you should either tag the user in an action or use the Requests API.