I not able to get user friends photos not profile picture. I can easily get profile picture
but not able to get user friend photos
I wrote this code but it is not working for me
FB.api('me/friends?fields=albums', function(response) {
for (var i=0; i<response.data.length; i++) {
var album = response.data[i];
FB.api('/'+album.id+'/photos', function(photos){
if (photos && photos.data && photos.data.length){
for (var j=0; j<photos.data.length; j++){
var photo = photos.data[j];
// photo.picture contain the link to picture
var image = document.createElement('img');
image.src = photo.picture;
document.body.appendChild(image);
}
}
});
}
});
Given all the required permission friends_photos,publish_stream
But still no luck .
Please help
Your graph url is off. To get a specific users photo album list you need:
{user_id}/albums?fields={list_of_album_fields}
In conjunction with
user_photos and/or friends_photos permissions
The Facebook graph explorer is an excellent tool for getting your graph url's right before coding. Try it out to verify this solution.
If you're looking to get all albums for all friends in a single pass you'll need to formulate this as either FQL or a batch request.
Related
I'm creating a website which show a single Facebook page content in the homepage (events basic informations, photos and the five latest instagram photos). Do I have to submit an App Review? If yes, what permissions do I need?
There is no FB login request in the site and no content is published using it. No FB/Instagram user information is shown.
Thanks,
Alessio
EDIT:
Here is my code. It's the same for every endpoint
function loadFacebookEvents(){
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
fbEvents=JSON.parse(this.responseText).data;
renderFacebookEvents()
//loadFacebookPhotos()
}
};
//events
xhttp.open("GET", "https://graph.facebook.com/thecagelivorno/events?time_filter=upcoming&fields=cover,description,end_time,name,start_time,ticket_uri,place&access_token=<my-access-token>", true);
xhttp.send();
}
I am trying to dynamically show photos on my moms wedding website that she adds to her photos albums on Facebook but I am having trouble getting the images to render and loop. My get request is working and currently pulling in the image link and the id of each photo in the album. This is an example of the json that is returned.
{
"photos": {
"data": [
{
"picture": "https://scontent.xx.fbcdn.net/v/t1.0-0/s130x130/13879222_1235179076493734_1631898598554522774_n.jpg?oh=e6d74e7731f374be63a378e99ef3a88e&oe=58111449",
"id": "1235179076493734"
}
Here's my code so far
$.getJSON('https://graph.facebook.com/102060453138941?fields=photos{picture}&access_token=xxx',
function(fbResults) {
for(var i =0; i < fbResults; i++) {
$('.gallery').append(
'<img class="" src="https://graph.facebook.com/'+ this.id +'/picture" />'
);
}
});
Try this:
for(var i = 0; i < fbResults.photos.data.length; i++) {
$('.gallery').append(
'<img class="" src="https://graph.facebook.com/'+ fbResults.photos.data[i].id +'/picture" />'
);
}
Btw, you will only get 25 entries with the API. You can set a limit with the limit parameter or you can use paging: https://developers.facebook.com/docs/graph-api/using-graph-api/#paging
If you need to get all entries at once, i suggest using paging with a recursive function.
Btw, you would need to add the User Token to each line, which is a very bad idea. The whole code should better go to the server and you should actually download the photos and cache them. Just saying.
I've been playing around with the FB SDK and some examples that exist for displaying FB photo albums, but the examples are much more complex than what I want to do. All I want to do is set the album id for a single album and pull in the contents. That's it, I can style everything after that, I just need to be able to view the contents of one album and pull in the caption for each photo.
I'm trying to create a WordPress album that does this; I can easily create the base of the widget, but I'm lost when it comes to API's.
Are there any examples out there that I've overlooked?
Does anyone know how to accomplish this?
hopefully this gives you a hint ;)
<?
if (empty($_GET['album'])) {
//get all albums of page
$graph_url = "https://graph.facebook.com/PressPlayPrague/albums? fields=id,link,name,cover_photo&limit=500";
$albums = json_decode(file_get_contents($graph_url));
$counted = sizeof ($albums->data); // get the number of albums for later use
//output all albums of given page
for($c=0; $c<$counted; $c++){
echo ("<div class='wrapper' <a href='?album=".$albums->data[$c]->id."'><div class='stack'><div style='width:180px; height:120px; display:inline-block; background-image:url(https://graph.facebook.com/".$albums->data[$c]->id."/picture?width=300&height=200); background-repeat:no-repeat;'> </div></div><div class='caption'>".$albums->data[$c]->name."</div></a></div>");
};
}else{
//get the album pictures replace the $_GET[album] with specific ID and remove the if clause to get only one album
$pic_url = "https://graph.facebook.com/".$_GET[album]."/photos?fields=picture&limit=500";
$pictures = json_decode(file_get_contents($pic_url));
$countpic= sizeof ($pictures->data); // get the number of pics for later use
for($p=0; $p<$countpic; $p++){
echo ("<img style='width:250px; max-height:167px;' src='https://graph.facebook.com/".$pictures->data[$p]->id."/picture' alt='".$pictures->data[$p]->id."'>");
};
}
?>
Im trying to get all the feed from my facebook page whit the javascript api.
Does anybody have a working example?
I have tryed but I cant get it right, I can get photos from albums, but I cant get the feeds and I dont know what Im doing wrong.
Any input really appreciated. Thanks!
I have this code:
Edit
OK, I have the code inside the init code like below and it doesnt work, it is not executing the code and I get no errors:
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<%=facebookAppId%>', // App ID
channelUrl : 'www.mypage.se/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true, // parse XFBML
oauth : true
});
// get the wall - feed
var page_id = '<%=facebookPageId%>';
FB.api('/' + page_id, {fields: 'access_token'}, function(resp2) {
if(resp2.access_token) {
alert(resp2.access_token);
FB.api('/page id/feed?access_token='+resp2.access_token, function(response) {
var ul = document.getElementById('feed');
for (var i=0, l=response.data.length; i<l; i++) {
var
feed = response.data[i],
li = document.createElement('li'),
a = document.createElement('a');
a.innerHTML = feed.message;
a.href = feed.link;
li.appendChild(a);
ul.appendChild(li);
}
});
}
});
};//end window.fbAsyncInit
// Load the SDK Asynchronously
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
</script>
So why can I have for example:
FB.api('/facebook id', function(response) {
alert('Your name is ' + response.name);
});
inside the init code and not the other code?
I would still need some structure help writing the callbacks out on the page so it looks good as it does on facebook.
I like to write out "message, picture, link, name" and maybe something else?
As I test to get the messages in this example and some of the callback is displayed as "undefined" on the page? Does that mean that the message doesnt have a value when I get it?
If so how can I not write out emty values - undefined ones?
Thanks!
UPDATE:
If I use this code then it reads the feed, but only if Im logged in to Facebook?
Im starting to get confused, since I think that the above code should work:
I first ask for the access token in the first call, and when I get the access token, then using it in the second call, what Im I doing wrong?
FB.api('/my userid or pageid/feed?access_token=the acces token that I get from the Graph API Explorer', {limit:5} , function(response){
if (response && response.data && response.data.length){
alert(response.message);
var ul = document.getElementById('feed');
for (var j=0; j<response.data.length; j++){
var feed = response.data[j],
li = document.createElement('li'),
a = document.createElement('a');
a.innerHTML = feed.message;
a.href = feed.link;
li.appendChild(a);
ul.appendChild(li);
}
}
});
I get a couple of undefined responses, how can I not write them out on the page?
How can I get the above code to work even if the user that visit the webpage is not logged in to facebook?
I make other calls that gets the photos of an album with no problem displaying them on the webpage, I dont understand why this is different?
If I want to set this up for a customer(when its finished) what are the steps I need to take? Getting a bit confused... :-)
I would like to get the message, picture, likes etc with the call
and display it like it is on the feed-wall, any suggestions here?
Ok, finally I got it working :-)
I got the right acces token and now I can display the feeds.
I got the right token here:
https://graph.facebook.com/oauth/access_token?client_id=myapp_id&client_secret=myapp_secret&grant_type=client_credentials
And now I can use the below code both for getting user feeds and page feeds, just changing the page_id!
var page_id = '<%=facebookPageId%>';
var pageaccessToken='xxxxxxxxxxxxxxxxxxxxxxxxxx';
FB.api('/' + page_id + '/feed?access_token='+ pageaccessToken, {limit:5} , function(response){
if (response && response.data && response.data.length){
var ul = document.getElementById('pagefeed');
for (var j=0; j<response.data.length; j++){
var feed = response.data[j],
li = document.createElement('li'),
a = document.createElement('a');
a.innerHTML = feed.message;
a.href = feed.link;
li.appendChild(a);
ul.appendChild(li);
}
}
});
Ok, now it works, but I get the undefined in the response?
What is causing this, is it because it is a empty value that I get in the response?
What can I do so it is not displayed on the page?
I don't understand what it is you're trying to do, I have the feeling though that you took the wrong path..
Let's start with the fact that you don't need any special permission or a page access token to query for the page feed, it's public and so all you need (quoted from the docs) is: "any valid access_token or user access_token".
Which means that if a user authorized your application, you then acquired an access token for him and can get any page feed.
For example try the southpark page feed in the Graph API Explorer.
Another issue is that asking the user for the "manage_pages" permission and then asking the graph api for the access token of your own app would not work. When a user grants your app to manage his pages that's exactly what you get, the permissions to his pages, and so this request for example: /southpark?feilds=access_token won't get you the token since you are not the admin of this page (even if you grant the app with the "manage_pages" permission).
Last thing is the use of window.onload, which in your situation just does not address the problem.
You don't want to execute that code when the window is loaded but when the facebook sdk is loaded and intialized. That's why facebook tells you to use the fbAsyncInit event.
The code you had before the window.onload is the correct form, what exactly do you mean when you say "it didn't work"? Did you get any errors? It did not get executed?
Edit
From what I understand you want to display the content of a facebook Page feed in your website.
As I already told you, all you need in order to get the feed of a page is an active access token ("any valid access_token or user access_token").
If you follow the Authentication doc, the App Login section you'll see that you can issue an access token for your facebook app. With that token you can then get the feed of ANY PAGE you want on the server side.
i have create Facebook app for my external website and i created an Access token for that app with the "offline" access Permission (access token) with that token, i placed the Login button in my site ,i'm trying to fetch the insights data's,of those who log in through my External website with Facebook while connect i'm passing the offline_access, Manage_page **read_insights** permission, though permission granted by the page Admins i'm difficult to fetch the insights datas.Please some one help me to figure this
aspx file
<fb:login-button scope="manage_pages,offline_access,read_insights" size="large" style="opacity:.01 !important; width:250px !important;line-height: 31px !important;margin-top:0 !important;" onlogin="getPages()"> Sign In With Facebook Account </fb:login-button></div>
scrpit file
window.getPages = function () {
loadingImage($('#divSettings'), true, ($('.divSettings').outerHeight() - 50) + 'px');
//test
FB.getLoginStatus(function (response) {
if (response.status.toString().indexOf("connected") > -1) {
//User is conected and granded perms Good to go!
// alert('1');
FB.api('/me/accounts', function (resp) {
//var txtpageid = document.getElementById('ctl00_contentPlaceHolderRight_txtFBPageID').value;
var txtpageid = $.trim($('#ctl00_contentPlaceHolderRight_txtFBPageID').val());
// alert(2);
$('#spnFBsignMo').removeClass('Dsh_pop_MessTxt Pop_Facebk_Txt_error').addClass('Dsh_pop_MessTxt Pop_Facebk_Txt');
// if (response.error && response.error.type == "OAuthException") { alert('OAuth2.0 Exception Access token Expired or AppID miss matched'+'<br>'+'please contact Admin'); }
if (txtpageid != '') {
FBactualID = '';
for (var i = 0, l = resp.data.length; i < l; i++) {
var page = resp.data[i];
if (page.id == txtpageid) {
FBactualID = txtpageid;
varFBResponseFlag = true;
//alert('inside the page id validation');
}
}
}
getCheckedValue(FBactualID);
// alert('3');
});
}
else {
//measures to take the users to login again
// alert('You must login to Facebook to Validate the Page ID');
$('#spnFBsignMo').removeClass('Dsh_pop_MessTxt Pop_Facebk_Txt').addClass('Dsh_pop_MessTxt Pop_Facebk_Txt_error'); //DSh_Input_cus DSh_Input_width
loadingImage($('#divSettings'), false, ($('.divSettings').outerHeight() - 50) + 'px');
alert(4);
}
}); // login sta
i'm just give a call to with my access token to fetch information
something like this
https://graph.facebook.com/246355928767490/insights/page_views&access_token=xxxxxx-xxx-xx&since=2011-12-25&untill=2012-01-04
The call to
https://graph.facebook.com/app_id/insights
Will only give you access to insights about your app, not "other's facebook page insights". If you are actually trying to get your own insights, check first that there is any data by going to the insights page and looking at your specific application here:
http://www.facebook.com/insights/
If you are trying to get the visiting, logged in user's Insight stats for pages they own, there are already a number of answers to this. You will need to troubleshoot whether your user is indeed logged in or not but the function you are using to retrieve insights should be right. Have a look at theses previous answers:
Facebook page insights via API
Facebook Insights for Page via the API