Facebook Mutual Friends API - facebook

Using the Facebook Graph API (v2.4), I can't seem to access any information about mutual friends, not even the total count.
Here's my graph query (User ID changed for privacy purposes):
https://graph.facebook.com/v2.4/123456789?fields=context.fields(mutual_friends)
The result I get is:
{
"context": {
"id": "dXNlcl9jb250ZAXh0OgGQBqWf9ZAHMZA1yjZBJZABsMDkDORNsle8wkS8Acci9r4FsOdyRVl1TSGSXAsofmlaWYS05piSZCV9F1QwNNs0L9XpNuGLAaLyMk8Fnaiwyxpm5shUZD"
},
"id": "123456789"
}
I tried using FB's iOS SDK to make the same query as well, but got the same result.
Any suggestions?

The all_mutual_friends, mutual_friends, and three_degree_mutual_friends context edges of the Social Context API were deprecated on April 4, 2018 and immediately started returning empty data sets. They have now been fully removed.

The {user_id} must be another user of your app, and the user access token you MUST use is from another user of your app.
Then
GET /{user_id}?fields=context{mutual_friends}&access_token={other_users_access_token}
should work and give results, if both users gave your app the user_friends permission.
See
https://developers.facebook.com/docs/graph-api/reference/user-context#Reading
https://developers.facebook.com/docs/graph-api/reference/user-context/mutual_friends/

function aa_mutl_frnd(x, row)
{
FB.init({
appId : '<?php echo get_option('_fb_apps_id');?>', //Facebook apps id using theme option
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.5' // use graph api version 2.5
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var accessToken = response.authResponse.accessToken;
console.log(':acc_tk:'+accessToken);
//////////////////////////////////////////////////////////
var data={
'action': 'wq_accss_tkn_gnrt',
'ddt' : accessToken
}
$.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function (response) {
console.log(':acc_tk2:'+response);
FB.api(
"/"+x+"",
{
"fields": "context.fields(all_mutual_friends)",
//"access_token": '',
"appsecret_proof": response,
},
function (response) {
console.log(response);
}
);
});
////////////////////////////////////
}
});
}
/// ajax part /////
add_action('wp_ajax_wq_accss_tkn_gnrt', 'wq_accss_tkn_gnrt');
add_action('wp_ajax_nopriv_wq_accss_tkn_gnrt', 'wq_accss_tkn_gnrt');
function wq_accss_tkn_gnrt() {
echo hash_hmac('sha256',$_POST['ddt'],'app_secret');;
die();
}

Related

Ionic app with option to attend facebook events

I'm doing a hybrid app using Ionic framework.
I want to implement a facebook event system to attend some events. I have this:
ngFB.api(
'/666945880120392/attending',
'GET',
{},
function(response) {
// Insert your code here
}
);
and I have ngFB in my app.js file like this:
ngFB.init({appId: '[APP_ID]'});
The error I have is:
openfb.js:256 GET https://graph.facebook.comundefined/?access_token=undefined net::ERR_NAME_NOT_RESOLVED
I was looking for a solution but no answer. I also try in the developer facebook console and the answer is correct:
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=666945880120392%2Fattending&version=v2.7
What it's happening? Thanks!
EDIT 1
Thanks to #e666 I did now this
ngFB.api({
method: 'GET',
path: '/666945880120392/attending'
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.error(error);
});
And the error now is that if I'm logged with an account always the return in the event is:
Object {data: Array[1], paging: Object}
And inside data is the name of another person, the unique person that is attending to the event and no the person who is logged with the access token.
Thanks!
EDIT 2
I opened another question because this is not the result than I hope, I need to go to an event, no to get the list of the people that goes to the event.
I have this now:
ngFB.api(
"/666945880120392/attending",
function (response) {
if (response && !response.error) {
console.dir(response);
}
}
);
the access_token is ok, and the response is
openfb.js:256 GET https://graph.facebook.comundefined/?access_token=EAABlK6y7pOUBAN3CPWdZB5FL…LCpXL9Bd3ELHQZAA6EJc6cCheAxUUnL59ZCZAf7aROCapJxiu991fxjDkxmMO651rfuREwZDZD net::ERR_NAME_NOT_RESOLVED
In the page of facebook https://developers.facebook.com/tools/explorer/?method=POST&path=666945880120392%2Fattending&version=v2.5
The response is
{
"error": {
"message": "(#299) Requires extended permission: rsvp_event",
"type": "OAuthException",
"code": 299,
"fbtrace_id": "G7HFJ48pbkx"
}
}
But I have this permission. Can someone help me please?
First, the path is undefined because you don't use correctly the method api. That is how you need to use it :
ngFB.api({
method: 'GET',
path: '/666945880120392/attending'
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.error(error);
});
Furthermore, you have undefined in access_token. As I looked in the library code, I am not sure that you can use this library without login as a user to have an access_token.
To have an access_token you need to use this function :
ngFB.login({scope: 'email'}).then(function(response) {
console.log('Access token' + response.authResponse.accessToken);
}).catch(function(error) {
console.error(error);
});
The access_token will then be automatically attached to request that you make with OpenFB.
You can find more complete examples of how to use the library is the github here : https://github.com/ccoenraets/OpenFB/blob/master/indexng.html

facebook users who r not my friends but using the app

The new Facebook graph api has divided friends into Friends & Invitable_friends .
me/friends/ returns my friends who are using the app while me/invitable_friends/ returns only the list of my friends who are not using the app.
But in my app,a user may interact with random users too who may not be on his friend list.
So how do i get user data for those who are using the app but are not on my friend list.
Thanks.
I went through the same issue and solved it like that: when a user starts your app by default you ask for permission to access his public profile information, which includes real ID, name and some other. You simply store the user data (id, name, etc.)in a database. This way you keep track of all the people using your app and they can interact with each other.
JavaScript Example:
window.fbAsyncInit = function () {
FB._https = true;
console.log('here');
FB.init({ appId: 'xxxxxxxxxxxxxxxx', cookie: true, xfbml: true, oauth: true, version : 'v2.0'});
Login();
};
function Login() {
FB.login(function(response) {
if (response.authResponse){
checkUser();
}
else{
console.log('User cancelled login or did not fully authorize.');
}
}
);
}
function checkUser() {
FB.api('/me', function(response) {
$.ajax({
type: "get",
url: "cgi-bin/checkUser.cgi",
cache: false,
data: {"user_name" : response.name, "u_id" : response.id, "link" : response.link},
success: function() //onSuccess,
{
console.log("user " + response.name + " checked!");
},
error: function()
{
console.log("Error with server connection on user checking");
}
});
});
}
I don't know in what language you are writing the app but I believe that in every language you should call the "login" function, which asks the user for permissions and basically gets access to info.
To get details of users who are using the app but are not your friends,call the following
graph.facebook.com/v2.0/
?ids={comma-separated-ids}&access_token=app_access_token&fields=name,picture
access_token is a must else graph-api returns following error-
The global ID xyz is not allowed. Please use the application specific ID instead.
This will work for all users who have authenticated the app no matter their authentication date,but for any non-app user graph-api returns above error message.

Facebook Authentication - adding permission for accessing email address

It might be a basic question.
And also my English is poor...
I have a web service which needs Facebook login.
With JavaScript SDK, I can get users' basic information such as user ID, name, gender.
And now there are about 10000 users.
This time, I need to access users' email address.
I know I can get email address from new users by adding scope attribute.
However, from existed users, though I can access email address, I don't know how to display authorization dialogue for permission to access email address.
Any idea? Thanks in advance!
Below is a code snippet that I have used. Basically check their permissions using /me/permissions and show a reauthenticate link
<script>
var FB_config = {
API_ID: 123123123123,
PERMISSIONS: "publish_stream,email",
};
jQuery(document).ready(function(){
// initialise FB
window.fbAsyncInit = function() {
FB.init({
appId : FB_config.API_ID,
status : true,
cookie : true,
xfbml : true,
oauth : true
});
FB.Event.subscribe('auth.statusChange', FBverifyLogin);
};
});
function FBverifyLogin(response) {
if (response.status === 'connected') {
checkPermissions(); // check permissions if the user is logged in
}
}
function checkPermissions(){
jQuery("#FBreauth").hide();
FB.api('/me/permissions', function(response) {
var permissions = FB_config.PERMISSIONS.split(",");
for(var i = 0; i < permissions.length; i++)
{
if(response.data[0][permissions[i]] == undefined || response.data[0][permissions[i]] != 1)
{
// user does not have full permissions, show reauth link
jQuery("#FBreauth").show();
break;
}
}
});
}
function FBreauth(){
FB.ui(
{
method: 'oauth',
display: 'popup',
app_id: FB_config.API_ID,
client_id: FB_config.API_ID,
redirect_uri: "http://www.facebook.com/connect/login_success.html",
scope: FB_config.PERMISSIONS
}
);
}
</script>
App permissions have changes. Please reauthorize this app

FB.getLoginStatus returns a session even after the user has logged out of Facebook

This is driving me crazy. A user logs into my Facebook app, does some stuff. Then goes to Facebook and logs out. I've got a little timer that calls FB.getLoginStatus() every once in a while to see if the user is still logged in. But whenever FB.getLoginStatus() gets called it returns a response with a session object. WTF? Shouldn't it return undefined/unknown?
I'm using the absolute most basic call to Facebook evar:
FB.init({
appId: 'MY APP ID',
cookies: false,
status: true,
xfbml: false
});
FB.getLoginStatus(function (response) {
if (response.session) {
console.info("Session exists");
} else {
console.info("Session empty");
}
});
setInterval(function () {
FB.getLoginStatus(function (response) {
if (response.session) {
console.info("Session exists");
} else {
console.info("Session empty");
}
});
}, 10000);
I checked and double checked the permissions that are being requested with the allow. I am only requesting email and sms. So.... any advice?
don't do it in the hard way. This situation has a simple solution, just add a "true" statement as a second parameter from the getLoginStatus method:
FB.getLoginStatus(function (response) {
if (response === undefined || response.status === 'connected') {
// Do something after logout
}
}, true);
Just be careful and aware that every call to this function will make your App to go to Facebook's servers and can give you a considerable amount of network load depending on your implementation. Hope this helps!
I had the same problem using your code.
The only way I got round it so far is to initialise the sdk each time login status is queried:
setInterval(function () {
FB.init({
appId : 'xxxx', // App ID
channelUrl : 'xxxx', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
oauth : true, // enable OAuth 2.0
xfbml : true // parse XFBML
});
FB.getLoginStatus(function (response) {
if (response.authResponse) {
console.info("Session exists");
} else {
console.info("Session empty");
}
});
}, 10000);
In this way I always get the correct session status. If there's a more efficient way to do this please let me know!
Try initializating FB with
status: false
So it will work as you expect. It worked for me
Are you sure the session always gets destroyed when the user logs out? Maybe it's just a var inside the session that changes if the users logs in or out....?

Extracting user id from Facebook Javascript SDK session object

I am using Facebook's Javascript SDK "FB.ui" to pull up an OAuth dialog. Once you click allow, I need to capture the session object, extract the user id and use it further in my script. For some reason I cannot get this working properly, I keep getting undefined, even though the session does exist.
<script src="http://connect.facebook.net/en_US/all.js"></script>
<div id="fb-root"></div>
<script type="text/javascript">
FB.init({
appId : '***************',
status : true,
cookie : true,
xfbml : true
});
FB.getLoginStatus(function(response) {
if (response.session) {
//do something
} else {
FB.ui({
method: 'oauth',
display: 'page',
scope: 'email',
perms: 'email'
},
function(response) {
alert(response.session.uid); //returns underfined
});
}
});
</script>
To get the userID:
FB.getLoginStatus(function(response) {
alert(response.authResponse.userID);
});
When you login from login button of facebook then it stores cookies in your system that contains your access token that is also unique. That cookies contains your facebook id also.
Check your response. I bet it says something like "display" must be one of "popup", "iframe" or "hidden". Apparently the oauth dialog cannot be called with the default "page" display. And display: 'iframe' requires that you include an access_token, which is why you're calling the oauth dialog in the first place >:l .
I don't think FB.ui can currently be used to get the oauth dialog, unless you want to use a popup, which most everyone has blocked nowadays.
So I got this to work as I would like. This may not be the best way, but after much digging around and frustration I hope this could help somebody with the same question get on the right track.
JS source:
FB.getLoginStatus(function(response) {
if (!response.session) {
//initiate FB OAuth js popup dialog
FB.ui({
method: 'oauth',
display: 'page',
scope: 'email',
perms: 'email'
},
function(response) {
if (response.session) { //if permission Allowed
var thesession = response.session;
var thesession = eval('(' + thesession + ')'); //decode json
//POSTing to local file get.user_graph.php, to fetch user info
$.ajax({
type: "POST",
url: "get.user_graph.php",
data: "client_id=<?php echo $appId; ?>&client_secret=<?php echo $secret; ?>&sessions="+thesession.session_key+"&type=client_cred&uid="+thesession.uid,
dataType: "text",
success: function(user_graph){
var user_graph1 = eval('('+user_graph+')');
alert(user_graph1.name); //users name
alert(user_graph1.id); //users id
alert(user_graph1.email); //users email
alert(user_graph1.link); //users profile link
}
});
} else {
//if permission Not Allowed
}
});
}
});
get.user_graph.php source:
//exchange our session for an access_token
define('POSTURL', 'https://graph.facebook.com/oauth/exchange_sessions');
define('POSTVARS', 'client_id='.$_POST['client_id'].'&client_secret='.$_POST['client_secret'].'&sessions='.$_POST['sessions'].'&type=client_cred');
$curl_token = curl_init(POSTURL);
curl_setopt($curl_token, CURLOPT_POST,1);
curl_setopt($curl_token, CURLOPT_POSTFIELDS,POSTVARS);
curl_setopt($curl_token, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($curl_token, CURLOPT_HEADER,0);
curl_setopt($curl_token, CURLOPT_RETURNTRANSFER,1);
$token = curl_exec($curl_token);
$token_decoded = json_decode($token,true);
//get the user graph (personal info)
$user_graph = file_get_contents("https://graph.facebook.com/".$_POST['uid']."?access_token=".$token_decoded[0]['access_token']);
echo $user_graph;