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
Related
What are the basic steps for setting up a pure server flow facebook SSO, the docs are as usual a little ambiguous?
I set up the flow with javascript popups only to later realise you are not allowed to customise the login buttons.. which when you stick them next google and twitter sso the signin box look terrible.
http://www.codecademy.com/ seem to direct to their own server which then forwards onto a URL like this:
https://www.facebook.com/dialog/oauth?response_type=code
&client_id=212500508799908&
redirect_uri=http%3A%2F%2Fwww.codecademy.com%2Fauth%2Ffacebook%2Fcallback&state=8aac5bc63c5afe8fbabe572021e7750579fefd898d7b4316&
scope=email%2Cpublish_actions
How is this URL being generated? In the facebook docs there is a function "getLoginUrl".. is this being called which generates the correct URL?
I tried directing the user directly from their browser to:
var href = 'https://www.facebook.com/dialog/oauth?' +
'client_id='+app_id+'&'+
'redirect_uri=http://www.mysite.net/authenticate_facebook.php&'+
'scope=email&';+
'state='+$('body').attr('unique');
But the at the facebook php then the following recieving code resulted in errors about the 'state' not matching... I am assuming that the state is not just a random value generate by my server and must be aquired from the facebook server?
require_once(WEBROOT_PRIVATE.'authenticate/facebook-php-sdk/src/facebook.php');
$config = array(
'appId' => 'xxx',
'secret' => 'xxx',
'fileUpload' => false,
'allowSignedRequest' => false
);
$facebook = new \Facebook($config);
$user_id = $facebook->getUser();
if($user_id)
{
try
{
$user_profile = $facebook->api('/me','GET');
}
catch(FacebookApiException $e)
{
error_log($e->getType(), 0);
error_log($e->getMessage(), 0);
}
}
SO, is this correct flow:
1 - Direct the user to my server facebook_auth.php
2 - facebook_auth.php generate the get url and forwards the user onto it
3 - The user, if required logs into facebook, allows my app
4 - my facebook_auth.php script then checks the tokens and talks server to server with facebook to verify the rest
5 - my website then logs the user in
I had a similar issue last week, and tracked it down to the state field being overwritten by multiple calls to getLoginUrl(). Each time you call getLoginUrl(), a new state token is generated in the SDK and stored in the $_SESSION (it's just a random value), so if you call it twice and the user uses the first link to log in, the second call will have reset the SDK's internal state token, and you will get this error in your logs.
The SDK looks for the same state token in the URL coming back after Facebook authorizes the user and redirects them back to your site, and if it doesn't match it will log this error (here's a link to the source).
I am using following php code :
$vk = new VK($app_id, $api_secret);
$user_wall = $vk->api('newsfeed.get', array(
//'owner_id' => $o->profile_uid,
'count' => 100,
'filters' => 'post,photo,wall_photo,friend',
'access_token' => $o->profile_token
));
echo '<pre>';
print_r($user_wall);
exit;
I am getting error when trying above code. I have successfully completed auth and stored user profile info in mysql table. I notice that when I see Api.Console permission in App> Setting, I see Access the Wall permission. But in application I used to retrieve data, I do not see this permission.
Error description : Permission to perform this action is denied
Error code : 7
The documentation is poorly described. Even which field is required or optional I can not determine. And what is difference between wall.get with filter "others" vs newsfeed.get ?
LOGIN CODE:
$AuthURL = $vk->getAuthorizeURL('notify,friends,photos,audio,video,docs,notes,pages,status,offers,questions,wall,groups,notifications,stats,ads,offline', $redirect_uri);
AUTH CODE:
$vk_code = $_REQUEST['code'];
$vk = new VK($app_id, $app_secret);
$access_token = $vk->getAccessToken($vk_code, $redirect_uri);
$uid = $access_token['user_id'];
$token = $access_token['access_token'];
$user_info = $vk->api('users.get', array(
'user_ids' => $uid,
'fields' => 'nickname, screen_name, sex, bdate (birthdate), city, country, timezone, photo, photo_medium, photo_big, has_mobile, contacts, education, online, counters, relation, last_seen, activity, can_write_private_message, can_see_all_posts, can_post, universities, counters'
));
First you must register application: vk.com/editapp?act=create
Then you need get authorize code. To do this follow link:
oauth.vk.com/authorize?client_id={APP_ID}&scope={API_SETTINGS}
Where {APP_ID} — your application id (see on app settings page),
{API_SETTINGS} — access rights requested by your app (through comma). If need infinite token use key "offline". For newsfeed need use key "wall,friends,offline".
Opens page. Copy string in URL AFTER #code=
Later you need get access token. Go to link:
https://oauth.vk.com/access_token?client_id={APP_ID}&client_secret={API_SECRET}&code={CODE}
Where {API_SECRET} — secret application key (see on app settings page),
{CODE} — code that was copied in step 2.
Copy access_token.
To get newsfeed data request link:
https://api.vk.com/method/newsfeed.get.xml?access_token={ACCESS_TOKEN}
Where {ACCESS_TOKEN} — token that was in step 3.
NOTE: USE HTTPS WHEN USING THIS ACTIONS
You need the following rights to call this method: wall and friends. (Read more on rights)
You must generate authorization with wall and friends...
https://oauth.vk.com/authorize?client_id=APP_ID&scope=wall,friends,offline
Just replace APP_ID with your appilication and then get your token
I am working on a script to pull recent Facebook posts/status updates for a specific using the Facebook API. I downloaded the FB SDK and searched all over the FB developers site, SO, and other places and it seems like the code below should accomplish what I need. However, although I am not getting an error, I am getting a NULL result.
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXX',
));
$fbApiGetPosts = $facebook->api('***My Name***/feed?limit=5');
var_dump($fbApiGetPosts["data"]);
I have tried using my two FB accounts,(my personal acct and a test account that I created just for this project) as well as one for my company and each time the response was NULL. When I misspelled the account name or used a name that did not exist I got an error, which seems to suggest the request is going somewhere.
In the code above, I also tried var_dump-ing $fbApiGetPosts and that result was also null
Can anybody see what the missing piece of the puzzle is?
I've not used the Facebook SDK, but here is how I retrieve Facebook posts:
$appId = 'XXXXXXXXXXXX';
$token = 'XXXXXXXXXXXXXXXXXXXXXXXX';
$posts = json_decode(
file_get_contents('https://graph.facebook.com/' . $appId . '/feed?
access_token=' . $token
)
);
var_dump($posts->data);
Hopefully this helps!
EDIT:
The token is just called an "Access Token". Here are the instructions for obtaining one. You might find the Access Token Tool helpful, but it's more for debugging existing tokens then creating new ones.
Just use the PHP SDK :
download link : https://developers.facebook.com/docs/php/gettingstarted/
Log your app :
$facebook = new Facebook(array('appId' => 'XXXXX','secret' => 'XXXXX'));
$facebook->setAccessToken('stored access token');
then :
$fbApiGetPosts = $facebook->api('/me/feed');
print_r($fbApiGetPosts);
Here is the Graph doc : https://developers.facebook.com/docs/graph-api/reference/
and the /feed ref : https://developers.facebook.com/docs/graph-api/reference/user/feed/
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.
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!