I'm using the Facebook JavaScript API and have gone over their documatation and cannot find the answer to the following question.
After a user has granted your application specific permissions (in this case read_stream), how do you obtain that particular data? To be more specific I am using the following as part of my login process:
function login(){
alert("[fbCommon]" + ' > login');
FB.login(function(response) {
if (response.authResponse) {
//console.log('Welcome! Fetching your information.... ');
alert("[fbCommon]" + ' > login > ' + 'Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
//console.log('Good to see you, ' + response.name + '.');
alert("[fbCommon]" + ' > login > ' + 'Good to see you, ' + response.name + '.');
});
} else {
alert("[fbCommon]" + ' > login > ' + 'User cancelled login or did not fully authorize.');
//console.log('User cancelled login or did not fully authorize.');
}
}, {scope: 'read_stream'});
}
Thanks for your help.
If all you want is their stream/feed data, change the
FB.api('/me', function(response) {...})
to
FB.api('/me/feed', function(response) {...})
You can use the trusty Graph Explorer to check this
Related
After successfully log in, i try to get a few fields from the user.
All of them are undefined other than the name and ID. The pop up indeed ask those permission which I granted.
Read all posts here and non offered a solution that work other than using tokens(?) which I am not sure how, and never see in FB examples.
FB.login(
function(response) {
if (response.status === "connected") {
console.log("connected");
console.log("Access Token: " + response.authResponse.accessToken);
testAPI();
} else {
console.log("not connected");
}
},
{ scope: "email,user_age_range,user_friends" }
);
function testAPI() {
console.log("Welcome! Fetching your information.... ");
FB.api("/me", function(response) {
console.log("name: " + response.name);
console.log("email: " + response.email);
console.log("id: " + response.id);
console.log("friends: " + response.user_friends);
console.log("age: " + response.user_age_range);
console.log("end");
});
}
If I print response i get an error :
Uncaught ReferenceError: respond is not defined
To anyone who wonder, first, the way to read it is :
FB.api('/me', {fields: 'name,email'}, (response) => {
console.log('name: ' + response.name);
console.log('email: ' + response.email);
});
Second, a few points to notice:
Remove your app on your facebook profiles (under Apps) otherwise every time you test, FB will automatically grant access even if you wish to add more permissions.
Remove cache from your browser every new test because FB will keep the previous state and changes will not take effect.(sometimes, even updated deployments will not)
You should deploy your website, with me - local host is not working with FB because they require https.
Testing Facebook is a little bit hassle.
How do I Extract Facebook profile picture timeStemp using the Facebook JavaScript API?
Following code i am using to get facebook profile-picture.
$scope.getInfo = function() {
FB.api('/me', 'GET', {fields: 'first_name,last_name,name,id,picture.width(100).height(100)'}, function(response) {
document.getElementById('status').innerHTML = "<img src='" + response.picture.data.url + "'>";
console.log(response)
});
}
or is there any way that i can get timestemp of profile picture ?? Please help me out :)
I have a django project which is intended for a facebook application. In the project, the invite friends module runs fine in localhost!
But while it is loaded in facebook application, the javascript responsible for displaying the friends list doesn't work. Although the libraries are properly loaded.
I don't know why it is so. May be some iframe problem.
Here is the javascript code
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({appId: '460948667348013', cookie: true});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
FB.api('/me', function(response) {
// alert('Your name is ' + response.name);
console.log('Your name is ' + response.name);
init();
});
} else if (response.status === 'not_authorized') {
alert('the user is logged in to Facebook, but has not authenticated your app');
} else {
alert('the user is not logged in to Facebook.');
}
});
}
function init() {
FB.api('/me', function(response) {
$("#username").html("<img src='https://graph.facebook.com/" + response.id + "/picture'/><div>" + response.name + "</div>");
$("#jfmfs-container").jfmfs({
max_selected: 15,
max_selected_message: "{0} of {1} selected",
friend_fields: "id,name,last_name",
pre_selected_friends: [1014025367],
exclude_friends: [1211122344, 610526078],
sorter: function(a, b) {
var x = a.last_name.toLowerCase();
var y = b.last_name.toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
});
$("#jfmfs-container").bind("jfmfs.friendload.finished", function() {
window.console && console.log("finished loading!");
});
$("#jfmfs-container").bind("jfmfs.selection.changed", function(e, data) {
window.console && console.log("changed", data);
});
$("#logged-out-status").hide();
$("#show-friends").show();
});
}
$("#show-friends").live("click", function() {
var friendSelector = $("#jfmfs-container").data('jfmfs');
$("#selected-friends").html(friendSelector.getSelectedIds().join(', '));
});
function sendRequest() {
var friendSelector = $("#jfmfs-container").data('jfmfs');
var sendUIDs = friendSelector.getSelectedIds().join(', ');
// Use FB.ui to send the Request(s)
FB.ui({method: 'apprequests',
to: sendUIDs,
title: 'My Great Invite',
message: 'Check out this Awesome App!',
}, callback);
}
function callback(response) {
// alert('callback called');
var friendSelector = $("#jfmfs-container").data('jfmfs');
var sendUIDs = friendSelector.getSelectedIds().join(',');
var uids = sendUIDs.split(',');
var query = '';
for(i=0;i<uids.length;i++){
if(i==0){
query = query + 'to[' + i + ']=' + uids[i];
}
else{
query = query + '&to[' + i + ']=' + uids[i];
}
}
console.log(query);
if(response){
// alert('successful');
window.location.assign("/?"+ query)
}
else{
alert('failure');
}
}
</script>
Please help ! I am stuck with this problem.
The issue may be SSL issues.
Facebook has made many changes in few months ago.
Also you need to keep update with Facebook developer blog.
So I am going to try to explain few things I suspected.
App on Facebook
Your Canvas Page should be https://apps.facebook.com/yourchoosenname
1a. Your Canvas URL should be https://yoursite.com/yourapplication/
Website
http://yoursite.com/
Page Tab
Your Secure Canvas should be https://yoursite.com/yourapplication/
Your Page Tab URL should be https://yoursite.com/yourapplication/
Your Secure Page Tab URL should be https://yoursite.com/yourapplication/
In this case you will need SSL Certificate for site so You can find reliable yet cheap digital certificate from Here .
This mandatory for any Application to work on facebook.
Hope this will help you and others
I am trying to access the Appcelerator Titanium SDK for ACS after authenticating a user through Social Integrations. I am using the following code, which is copied line for line from the ACS Kitchen Sink example application, and I still cannot get it to work. The call to log the user in is successful, but the subsequent call to PhotoCollections API returns "404 - Failed to Authenticate user".
I am using oauth 3 legged authentication with the User Authentication Scheme set to Authorization Server. I have also tried other ACS APIs, without any luck. Please help! I've been stuck on this for 2 weeks now.
fb.addEventListener('login', function(e) {
if (e.success) {
Cloud.SocialIntegrations.externalAccountLogin({
type : 'facebook',
token : fb.accessToken
}, function(e) {
if (e.success) {
var user = e.users[0];
Cloud.PhotoCollections.create({
name : 'Party Pictures'
}, function(e) {
if (e.success) {
var collection = e.collections[0];
alert('Success:\n' + 'id: ' + collection.id + '\n' + 'name: ' + collection.name + '\n' + 'count: ' + collection.counts.total_photos + '\n' + 'updated_at: ' + collection.updated_at);
} else {
alert('Error:\n' + ((e.error && e.message) || JSON.stringify(e)));
}
});
} else {
alert('Error: ' + ((e.error && e.message) || JSON.stringify(e)));
}
});
}
});
Turns out that multiple posts online were wrong about the server configuration needed to do this. Make sure that your "User Authentication Scheme" is set to "API Server" and not "Authorization Server". It turned out to be that simple for me.
I'm playing around with the Facebook API in Javascript to try and pull posts from Facebook. I copied code from this page in the documentation. The function will access the page given and actually produce a response. But, response.length and response[i] are returning as "undefined" thus making it impossible for the loop to work. Why is this happening?
<button onclick="ShowMyPosts()">Show posts</button>
<script language="javascript" type="text/javascript">
function ShowMyPosts() {
FB.api('/TheWalkingDeadAMC/posts', { limit: 3 }, function(response) {
console.log(response)
console.log(response.length)
for (var i=0, l=response.length; i<l; i++) {
var post = response[i];
console.log(post)
if (post.message) {
console.log('Message: ' + post.message);
} else if (post.attachment && post.attachment.name) {
console.log('Attachment: ' + post.attachment.name);
}
}
}
)
console.log("successfully ran function")
};
</script>
How I get an access token. Is this incorrect?
FB.login(function(response) {
if (response.authResponse) {
var access_token = FB.getAuthResponse()['accessToken'];
console.log('Access Token = '+ access_token);
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}{scope: 'read_stream'}
});
When you query the Graph API - most results will be inside data key...
Using Graph API Explorer you can test your call and see what is the result without having to write a single line of code.
This is a direct link to your Graph API call test. as you can see data holds the posts.
So, you need to loop through response.data and not response.
The code should look like this:
function ShowMyPosts() {
FB.api('/TheWalkingDeadAMC/posts', { limit: 3 }, function(response) {
console.log(response.data);
console.log(response.data.length)
for (var i=0, l=response.data.length; i<l; i++) {
var post = response.data[i];
console.log(post)
if (post.message) {
console.log('Message: ' + post.message);
} else if (post.attachment && post.attachment.name) {
console.log('Attachment: ' + post.attachment.name);
}
}
}
I think the issue was that my FB.login was incorrect. This is the correct code below:
FB.login(
function(response) {
if (response.authResponse) {
var access_token = FB.getAuthResponse()['accessToken'];
console.log('Access Token = '+ access_token);
FB.api('/me', function(response) {
console.log('Good to see you, ' + response.name + '.');
});
} else {
console.log('User cancelled login or did not fully authorize.');
}},
{scope: 'read_stream'}
);