To post a photo to logged in user's photos these are the api function parameters:
$result = $facebook->api('/me/photos/', 'post', $attachment);
I would like to post a photo in offline mode using this:
$result = $facebook->api('/'.$user_id.'/photos/', 'post', $attachment);
But this does not work, because I think user_id is read as a album id.
What is the proper url for posting to user's photos when I have a user id?
Where can I find the list of all these "url"s for different graph actions?
Note:User has granted publish and offline permissions to the app.
As long as the token is active you can use me so $result = $facebook->api('/me/photos/', 'post', $attachment); should still work back when offline_access was possible. Even now with 2 month tokens you can use me, again as long as the access token is valid.
Related
I know how to make a post to a Facebook page via the API using PHP SDK, that is done like this:
$facebook->api('/xxxxxxxxxxx/feed', 'post', array('message'=> 'Hello world!', 'cb' => ''));
Where xxxxxxxxxxx is page id ;)
But doing that, I post to that page as me, Jamie, and not as the Page itself (admin).
So how do I post as Admin/Page instead of myself?
Thanks you for your time!
ANSWER (for lazy people):
First of all you need to make sure you have access to manage pages for user, ex:
<fb:login-button autologoutlink="true" perms="manage_pages"></fb:login-button>
Now you also gets a special token for every page user have access to once you get them.
PHP SDK Example:
//Get pages user id admin for
$fb_accounts = $facebook->api('/me/accounts');
//$fb_accounts is now an array
//holding all data on the pages user is admin for,
//we are interested in access_token
//We save the token for one of the pages into a variable
$access_token = $fb_accounts['data'][0]['access_token'];
//We can now update a Page as Admin
$facebook->api('/PAGE_ID/feed', 'post', array('message'=> 'Hello!', 'access_token' => $access_token, 'cb' => ''));
Check out this note regarding your question: http://developers.facebook.com/docs/api > Authorization > Page impersonation.
Long story short, you need the manage_pages extended permission.
And btw, have you checked this first ?
I have developed an app so that users can upload images to a page's album. I do not want users to have to authorize and so I used the access token from the app to upload the images for them. This works fine if the admin is logged in and the access token hasn't expired - it only seems to last for a couple hours. I have looked through stackoverflow and facebook and tried many different things and nothing seems to work. Here is some of the code I am using:
try {
$facebook->setAccessToken("APP_ACCESS_TOKEN_FROM_GRAPH_API_TOOL");
$token_url="https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=fb_exchange_token&fb_exchange_token=".$facebook->getAccessToken();
$page_id = 'PAGE_ID';
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$page_info['access_token'] = $params['access_token'];
$page_info = $facebook->api("/".$page_id."?fields=access_token");
if( !empty($page_info['access_token']) ) {
$photo_details['access_token'] = $page_info['access_token'];
$upload_photo = $facebook->api('/'.$page_id.'/photos', 'post', $photo_details);
Am I dreaming? Is this possible and I just screwed up the code? Any help would be much appreciated even just to point me in the right direction...
Currently, your code uses your own Access Token, not the App Access token.
To get the app access token, you need to make a get request to https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials as documented at https://developers.facebook.com/docs/technical-guides/opengraph/publishing-with-app-token/
in your implementation, and assuming that APP_SECRET and APP_ID are defined as constants, you'd get the app access token through :
$appAccessToken = str_replace("access_token=","",$facebook->api("https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials"));
$facebook->setAccessToken($appAccessToken);
Hey all,
I've made a facebook application for a contest, which allows users to upload their photo. Once uploaded, the photo gets posted to a dedicated album on their profile. Once the photo is there, the users should collect as many likes as possible.
Currently i have tried both the Facebook Graph API and Facebook FQL to retrieve the amount of likes, but the retrieved likes don't always match the likes on a users profile.
Some users claim to have more then 200 likes, yet the API only returned a maximum of 101 likes so far.
All users are requested to grant the application the following permissions:
user_hometown, publish_stream, read_stream, user_photos and offline_access
Using Facebook PHP SDK 3.0.1 I tried this FQL query to collect the amount of likes of a photo:
# fql query
$fql = "SELECT object_id FROM like WHERE object_id=" . $photo_id;
# api request
$request = array(
'method' => 'fql.query', 'query' => $fql
);
# run batch request
$likes = $this->facebook->api($request);
# return like count
return count($likes);
I also tried the following Graph API request (Also with Facebook PHP SDK 3.0.1) to collect the amount of likes of a photo:
$likes = $this->facebook->api($photo_id.'/likes');
return count($likes['data']);
Strangely, neither seem to return correct results. I can understand if the API is slightly inaccurate, but according to the API some pictures recieved 100 likes this morning and then 0 likes a few hours later.
Does anyone have any ideas what i might be doing wrong? Do the photo's and their albums need to be public? Do the people who liked the photo need to have a public profile in order to show up in the API? Do i need to request additional permissions?
Any help or suggestions will be greatly appreciated!
Just had the same problem here. The likes seems to be paginated by hundreds by default. Override this setting by using the "LIMIT" query option.
Does anyone have any ideas what i might be doing wrong?
yes, I believe that it is against Facebook policy to use their likes plugin for a contest. See: https://www.facebook.com/promotions_guidelines.php
# fql query
$fql = "SELECT like_info FROM photo WHERE object_id=" . $photo_id;
# api request
$request = array(
'method' => 'fql.query', 'query' => $fql
);
# run batch request
$likeInfo = $facebook->api(array(
'method' => 'fql.query', 'query' => $fql
));
var_dump($likeInfo);die;
=> get like info of photo
brand new here.. I've looked everywhere, couldn't find an answer.. :/ Hopefully someone here can help me out. I'm using the graph api to upload a photo, and tag the user in it. This is currently working great. I would like to show the user the image, in the album, so I need the image id to generate the url. I cannot figure out how to do this!! Below are the lines of code that upload/tag the photo, to my app's album. What do I need to do to get this image id? Thanks a lot! -Tim
$user = $facebook->getUser();
function post_image($facebook, $user,
$image_path){
try{ $tag = array(
'tag_uid' => $facebook->getUser(),
'x' => 0,
'y' => 0
);
$tags[] = $tag;
$image = array(
'message'=> 'Your Banner Photo', 'tags' => $tags,
);
$facebook->setFileUploadSupport(true);
$image['image'] = '#'.realpath($image_path);
$facebook->api('/me/photos', 'POST', $image);
echo "";
return true;
}catch(FacebookApiException $e){
echo $e;
return false;
} }
Sadly, you have to do it as a separate query. You have to get the users galleries (first graph API call). then find the gallery ID you want and request the photos (second graph API call) then parse through the photos to find the one you want. Finally you have to request that photo.
-------------------Update:---------------------------
I'm not a very proficient php developer, but i use facebook api all the time with javascript or actionscript. The basic premise is to call the graph api, and it will return a json object of the result. Use getFile or Curl to load the graph api responses.
You can test your graph api calls here:
https://developers.facebook.com/tools/explorer/?method=GET&path=663032752
so try and walk that through from the graph api. Get the user galleries, then get the photos, then get the one you want.
To get the albums, call
"https://graph.facebook.com/me/albums?access_token="._accessToken;
To get the photos in an album you call it using:
https://graph.facebook.com/"._albumID."/photos?access_token="._accessToken;
And then to get the individual photo you would get the photo's ID property and call that.
sometihng like:
"https://graph.facebook.com/".your_photo_id;
I think using CURL is a good approach, but I'm weak on php, so use what you are comfortable with.
You have the photo ID in facebook API response, if it is uploaded successfully:
$result = $facebook->api('/me/photos', 'POST', $image);
echo $result['id'];
So in my app, I do this:
FB.api('/me', function(response) {
alert(response.name);
});
This gives me the users name IF they are logged into FB through my site. Now, is there a way I can get a unique ID for them as well? That way everytime they post something on my site, I can store their post along with their FB name AND Id, and then I know which posts they have made in the past when they are connected to their FB through my site.
You can use response.id to get the Facebook Users unique id.