Add an Album to Page using API (php) - php

I am trying to create an album for a page, however I get the following error;
[15-Sep-2011 22:38:14] PHP Fatal error: Uncaught OAuthException: (#100) Invalid ID for album owner
thrown in C:\inetpub\wwwroot\Diveengine\v3\facebook\base_facebook.php on line 988
the code is as follows
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => "aaaaaaaaaaaa",
'secret' => "aaaaaaaaaaaaaaaaaaaaa",
));
$facebook->setAccessToken("aaaaaaaaaaaaaaaaaaaaa");
$me = $facebook->api('/me');
//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);
//Create an album
$album_details = array(
'message'=> 'test album',
'name'=> 'Test Album'
);
$create_album = $facebook->api('/pageid/albums', 'post', $album_details);
// I have the page ID
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
I have been able to create an album for my profile, however I want this for the page.

ok dude, it took long enough to figure this out, but here goes. Your code is probably fine, but the trick is getting the right access token. Originally, I thought I had to create a page access token (using the graph api explorer) with all the frills that would allow me to do what I wanted.
Turns out, all you need to do is this:
1) go to https://developers.facebook.com/tools/explorer
2) it'll probably auto populate your facebook id, but affix /accounts at the end there so it'll look like this:
#yourfacebookidnumber/accounts
or you can just change it to
https://graph.facebook.com/me/accounts
you'll get a JSON dump of a list of all the fan pages you're an administrator to. Find the fan page you want to post to (you need to be an administrator of that fan page).
Grab the access token specified there, use it in your code, and Bob's your uncle!

Related

Facebook Php Api basics

Im getting started with the php sdk, and struggling to understand a few things (I have a basic example below - but everytime the pic goes to MY wall and not the fan page)
Code:
require_once("facebook.php"); // set the right path
$config = array();
$config['appId'] = 'app id';
$config['secret'] = 'app secret';
$config['fileUpload'] = true; // optional
$fb = new Facebook($config);
$params = array(
// this is the access token for Fan Page
"access_token" => "I create this via the graph api (select App and click get token), I select publish stream and photo_upload permissions.",
"message" => "test message",
"source" => "#" ."photo.png", // "#" .
);
try {
// **** is Facebook id of Fan page
$ret = $fb->api('/****/photos', 'POST', $params);
echo 'Photo successfully uploaded to Facebook Album';
} catch(Exception $e) {
echo $e->getMessage();
}
the picture keeps going to MY wall (logged in user) and not the fan page, is that because im getting a token for the currently logged in user (me) instead of the fan page? and if so how do i generate a token for the fan page whilst logged in as the developer? (is it by putting the fan page id into the Get bar?)
Apologies for the noob questions but the facebook documentation sucks (until u understand the core functionality of course). Most tutorials work once u know how to use the graph API - none ive looked actually EXPLAIN how to use the graph to generate correct tokens, etc.
\POST /<ID>/photos — this will post on the <ID>'s wall only, so please double-check the <ID> that you are using.
The access token will depict that on behalf of whom the photo would be posted on the <ID>'s wall. If you use the user access token, the photo will be published on behalf of the user and if page access token is used, it will be published on the page's behalf itself.
You can get the page access token of your pages by-
\GET /me/accounts?fields=access_token
(permission required: manage_pages)
Demo

Post Photos as Fan Page not as User (Post as User Working 100%)

I´m trying to post Photos on my Fan Page using Facebook API but the Posts appears as my User and not as Fan Page it self.
I know i should use the Page Access Token instead of the App User Access Token but when i put the Page Access Token i get:
Fatal error: Uncaught OAuthException: A user access token is required to request this resource.
What should i do to post the Image as Fan Page itself not as the app Owner ?
Here´s the Working Code:
require_once('src/facebook.php');
$pageid = "xxxxxxxxxxxxx";
$accessToken = "xxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
"appId" => "xxxxxxxxxxxxxx",
"secret" => "xxxxxxxxxxxxxxxxxxxx",
"cookie" => true,
));
$args = array(
'access_token' => $accessToken,
"url" => "http://example.com/image.jpg",
"message" => "Picture Caption",
);
$facebook->api("/$pageid/photos?access_token='.$accessToken","post",$args);
I just figure it out what was wrong. I was using wrong Page Access Token, the right way to get the right Page Access Token is through the following link:
https://graph.facebook.com/me/accounts?access_token=USER_ACCESS_TOKEN
You can even try the new Access Token on Debug Tools:
https://developers.facebook.com/tools/debug/
If you do that you´ll see a field named "Profile ID", this field has to be the same as your Fan Page ID, and probably will.
Now all my post appear as my Fan Page.
Hope someone else find this usefull.

Post to facebook page on behalf of user who is not a page admin

We are trying to build a facebook application that posts a message from a user to a facebook business page (which the user is NOT an admin of.) We need the post to be from the USER, not the page or the page admin. We are using the facebook PHP SDK and so far have accomplished getting the user's message to post to their own feed. (Using an App Access Token.) Though when we try to post the message to the facebook page (not owned by the user) we get this error:
Fatal error: Uncaught OAuthException: (#200) The user hasn't authorized the application to perform this action thrown in
Is it possible to post to a facebook page, not owned by the user on behalf of the user? We are admins on the facebook page, but if we post using the Page Access Token then the post is no longer authored by the user, but instead the facebook page or page admin.
Here is a snippet of our code to see how we're going about this:
Posting to user's feed as user (works)
$fbpage = "/$fbid/feed";
$attachment = array(
'message' => $msg,'from' => array('name' => $name,'id' => $fbid));
$result = $facebook->api('/'.$fbpage.'?access_token='.$accesstoken, 'post', $attachment);
Posting to facebook page as user (doesn't work)
$fbpage = "/johnsjoint/feed";
$attachment = array('message' => $msg,'from' => array('name' => $name,'id' => $fbid));
$result = $facebook->api('/'.$fbpage.'?access_token='.$accesstoken, 'post', $attachment);
After a lot of research, it appears that this isn't possible. If anyone knows otherwise, please post here.
Not sure if its the exact same issue, but this might shed a light over your problem. See
"Posting to Facebook fan page feed as user through PHP SDK"

Facebook FQL for fetching photo

My original point was to fetch the links of every photos of a facebook photo album, so I am trying to execute this FQL query:
select link from photo where aid=xxxxxxxxxxx
I want to execute it from php so I first tried to run the Facebook FQL sample from http://developers.facebook.com/docs/reference/fql/
But I can't figure out what is this line about:
$code = $_REQUEST["code"];
When is that $_REQUEST["code"] set ?
Any other simple ideas to fetch photo links of a given photo album?
The $_REQUEST variable referenced in the documentation pertains to the access token that the client (the website visitor) issues to your server once they grant permission for your to app to use their information. If you are accessing publicly accessible information you'll need an "app login" access token.
You should be using the php-sdk so getting that code isn't necessary because it handles all of that for you once you initialize a facebook instance with your app secret/id.
When I did something similar I did it this way:
require('src/facebook.php');
$config = array(
'appId' => 'YOURAPPID',
'secret' => 'YOURAPPSECRET',
'cookie' => true
);
$facebook = new Facebook($config);
$query = urlencode('select link from photo where aid=xxxxxxxxxxx');
try {
$fbData = $facebook->api("/fql?q={$query}");
} catch (FacebookApiException $e) {
$fbData = $e->getResult();
}
If you are accessing private information you'll need to follow the instructions on how to get the access token code. You can see an example of how to get that within the "example" folder of the sdk.

Get all the albums from a group i manage

I've manage a martial arts school's website, the schook also has a facebook group where friends and students can publish their news and photos.
I would like to fetch the group's albums and display their pictures in the website of the school.
So I tried this short script:
<?php
require_once(__DIR__.'/lib/facebook.php');
$facebook = new Facebook(array(
'appId' => 'API_KEY',
'secret' => 'API_SECRET',
));
$group = $facebook->api('GROUP_ID');
echo $group
?>
Which is raising this error in the app's log
Wed Aug 10 01:16:30 2011] [error] [client 127.0.0.1] PHP Fatal error:
Uncaught CurlException: 6: name lookup timed out\n thrown in
'./lib/base_facebook.php on line 814
So I wonder if this is the way to do it or if I should try getting the albums from elsewhere...
I find the documentation in developers.facebook.com scarce or confusing.
This is how I get my albums, I think the group album must be similar.
create a Facebook application. If you don’t already have one usable for this project create it on http://developers.facebook.com/setup/. You will own App Name, App URL, App ID, and App Secret get access code, go to:
https://www.facebook.com/dialog/oauth?client_id=Your_App_ID&redirect_uri=Your_App_URL&scope=read_stream,publish_stream,offline_access
You must allow the application to gain access to your account. (allow access to pictures to)
After you click allow, your browser will be redirect to Your_App_URL with additional URL attribute, it will look something like:
Your_App_URL?code=long_facebook_code
get offline access token, go to
https://graph.facebook.com/oauth/access_token?client_id=Your_App_ID&redirect_uri=Your_App_URL&client_secret=Your_App_Secret&code=long_facebook_code
the page will display something like
access_token=123456789|99bdea74e40ecc75530b7c45-132456798|dTntC8lVyR84eBxK1TS9ws2s_s0
Note: make sure there are no word “expire=” in the end of the token (the bold text)
copy your offline token (only the bold text) and use it, it permanent token and never change event if you change your application name.
then I use a script in PHP (PHP SDK 3.0.):
require 'src/facebook.php';
$appId = 'Your_App_ID';
$secret = 'Your_App_Secret';
$access_token = 'Your_access_token';
$facebook = new Facebook(array('appId' => $appId, 'secret' => $secret, ));
$facebook->setAccessToken($access_token);
to see available albums:
// bring the id, name and number of pictures.
$albums = $facebook->api('/me/albums?fields=name,count');
print_r ($albums);
to read the album:
$album_ID = "123456..."; //album ID
$list_pictures = $facebook->api('/'.$album_ID.'/photos', array('access_token' => $access_token));
print_r ($list_pictures);
You can not use /photos and /albums for group by Graph API.
First always return empty data, second - error "(#3) App must be on whitelist"
I created a python project to resolve this problem for a friend. You can't access the albums without being a trusted Facebook app, but you can access the feed, which contained the bulk of the images I wanted to grab.
This will download and archive everything (which is what they wanted) but I thought it might help you as well.
https://github.com/danfunk/ethans_fight

Categories