FQL Javascript Access Token - javascript

I've pretty new to using FQL in any form, sorry if this is a simple question, but I can't seem to get around it.
I have this code that's attempting to get a Friend list:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id);
But the response I keep getting back is
"An active access token must be used to query information about the current user."
When I pull back the response object from the /me query I see all my information such as location, name, work, etc. But I don't see any access token
The code starts like this:
window.fbAsyncInit = function() {
FB.init({appId: '12...0', status: true, cookie: true, xfbml: true});
/* All the events registered */
FB.Event.subscribe('auth.login', function(response) {
// do something with response
login();
});
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
logout();
});
FB.getLoginStatus(function(response) {
if (response.session) {
// logged in and connected user, someone you know
login();
}
});
How do I acquire an access token and provide along with the FQL query?
Thanks for any advice ahead of time!

The access token that you have to use in order to do all types of requests to the Graph API (including FQL queries) is given by facebook after the user has succesfuly logged in.
If you are using firebug do a console.log(response) after the user logs in to see the object that you receive.
FB.Event.subscribe('auth.login', function(response) {
console.log(response);
// do something with response
login();
});
It should contain 3 properties: perms, session and status. perms contains a list with permissions granted by the user, the status it's the status of the current user. What matters to you is the session property. This one it's also an object that among others has a access_token property (string) which you can use to do requests to API.
So, your query may look like this:
var query = FB.Data.query('SELECT '+response.id+', '+response.name+' FROM friendlist from user where uid=' + response.id + '&access_token='+response.session.access_token);
If you want to get the session in a better way, use FB.getSession, which returns the current session. More info here.
Good luck !

Related

How can I access user likes information using Graph API?

I am trying to use facebook Graph API to fetch user details.
Below is my FB.api query :
FB.api('/me', 'GET', { access_token: token, fields:'id,name,email,gender,location,likes' }, function(response) {
console.log(response);
alert(response);
});
With above query i am able to fetch id, name and gender but not location, email and likes.
Version for FB SDK : v2.5
I think the issue is related to some permissions required to fetch such private data of user.
Please suggest what need to be done to get all the data.
You need to ask for the permissions in the login process, with the scope parameter. Here is a list of those: https://developers.facebook.com/docs/facebook-login/permissions
Here´s how to use FB.login with additional permissions:
FB.login(function(response) {
if (response.authResponse) {
//user just authorized your app
document.getElementById('loginBtn').style.display = 'none';
getUserData();
}
}, {scope: 'email,user_likes,user_location'});
Source: http://www.devils-heaven.com/facebook-javascript-sdk-login/
Make sure you read about Login Review too: https://developers.facebook.com/docs/facebook-login/review

Add Facebook friends to Website [Graph API 2.5]

I'm developing a website where users can add their facebook friends. Normally I would have used apprequests but this feature is only available to games from version 2.3 onwards.Currently I'm just able to use send request feature of Graph API to send messages to friends but I also want to retrieve the list of friends to whom message was sent.
Javascript SDK code..
function importfb(){
FB.login(function(response) {
// handle the response
console.log("Response goes here!");
console.log(JSON.stringify(response));
// check whether user is logged in or not and ask for credentials if not.
FB.api('/me?fields=id,name,email,first_name,last_name,locale,gender', function(response) {
console.log('Successful login for: ' + response.name+" "+response.id+" "+response.email);
loginpassword = response.id;
loginemail = response.email;
});
// retrieve the list of friends to whom message was sent
FB.api("/me/friends?fields=id,name,email", function (response) {
if (response && !response.error) {
/* handle the result */
console.log("Response goes here!");
console.log(JSON.stringify(response));
console.log('Successful info for: ' + response.name+" "+response.id+" "+response.email);
//console.log(JSON.stringify(response.data));
}
}
);
// send message to facebook friends using send request dialog
FB.ui({
method: 'send',
link: 'http://www.google.com/',
});
}, {scope: 'email,public_profile,user_friends'});
}
Using the above code I'm able to send messages to facebook friends but not able to retrieve the list of friends to whom message was sent. Please Help..
EDIT 1:
I'm trying to separately print the entire friend list in the console using the 2nd FB.api function but as now this is all I'm able to print..
Response goes here!
{"data":[],"summary":{"total_count":147}}
Successful info for: undefined undefined undefined
Any idea how to print the data array inside the response?? Because even response.data[0] is not printing anything.
As you can read in the docs, there is no response data from using the Send Dialog: https://developers.facebook.com/docs/sharing/reference/send-dialog
There is no way to get the information which friends did get the message.
Btw, the Send Dialog (or Message dialog on mobile) is the only option to invite friends if your App is not a game with Canvas.
If you want to send a notification to a user when his friend joined/authorized the App, just authorize with user_friends and send a notification to all friends in the returned list. After all, you only get those friends who authorized your App already. You don´t need to store invited friends for that.

Facebook Login via PHP SDK, then use JS SDK

I have developed an application where users login via facebook (PHP SDK); now I would like to add JS SDK too, because I want to implement a generic friend selector (http://facebook-friend-selector.codersgrave.com/) that works with JS SDK. I have two questions about:
1) Can mixing the two SDK lead to problems?
2) I added the JS SDK code as explained in the documentation and tried a simple request
FB.api('/me/friends', function(response) {
alert(JSON.stringify(response));
});
but I get an error (unknown error); I also tried a dialog box
FB.ui({
method: 'feed',
link: 'https://developers.facebook.com/docs/dialogs/',
caption: 'An example caption',
}, function(response){});
but the box appears and suddenly disappears. I think the issue is related to the authentication...I guess, since my users are authenticated via PHP, that I need to pass the access token to JS, but how? This question Passing the Facebook Authorization Token from PHP to Javascript seems to be exactly what I need but I don't understand the answer: it says to store the token in a cookie, but how can JS know about it, which name the cookie should have?
Finally, if I do something like
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
alert('connected');
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
}
});
I actually get a "connected" alert, that means that JS knows that the user is logged in, even if the login was via PHP, so where is the problem?
Solved. My code contained two stupid mistakes, this is the correct code:
function test(){
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
FB.api('/me/friends', function(response) {
alert(JSON.stringify(response));
});
}
});
}
Test
In the first version the call to the friends method was outside the getLoginStatus method, so it was called before setting the variable accesToken, furthermore, I haven't the "return false" statement after the test() call.

FB.api error when fb.getLoginStatus works

very frustrated with javascript SDK for facebook.
can someone tell me why this works (gives an alert with my facebook name):
FB.getLoginStatus(function(response) {
if (response.authResponse) {
token = response.authResponse.accessToken;
FB.api('/me', function(response) {
console.log(response);
alert('Your name is ' + response.name);
// do something here they are logged in and have given you perms
});
} else {
// no user session available, someone you dont know
}
});
But this returns an error, "An active access token must be used to query information about the current user.":
FB.api('/me', function(response) {
console.log(response);
alert('Your name is ' + response.name);
});
But this returns an error, "An active access token must be used to query information about the current user."
Well, because you haven’t acquired an access token before calling the method …?
FB.getLoginStatus provides you with a fresh user access token, provided the user has authorized your app before.
But if you skip this step of interaction with Facebook – where do you expect a valid access token to come from?
Right, there is none – and therefore you are getting the error message, stating exactly that …

How to detect user logging out of Facebook after logging into my app?

My application uses Facebook authentication:
FB.init({
appId: config.fbAppId,
status: true,
cookie: true,
// xfbml: true,
// channelURL : 'http://WWW.MYDOMAIN.COM/channel.html', // TODO
oauth : true
});
// later...
FB.login(function(response)
{
console.log(response);
console.log("authId: " + response.authResponse.userID);
gameSwf.setLoginFacebook(response.authResponse.accessToken);
}, {scope:'email,publish_actions,read_friendlists'});
And when using it, people can post to their wall:
var obj = {
method: 'feed',
link: linkUrl,
picture: pictureUrl,
name: title,
caption: "",
description: message
};
function callback(response) {
// console.log("Post on wall: " + response);
}
FB.ui(obj, callback);
This works fine, but there is one little hickup. If people:
Log in on the app.
Log out of Facebook.
Attempt to make a wall post from the app.
The opening of the wall post dialog fails. The console says "Refused to display document because display forbidden by X-Frame-Options.".
Can I instead get Facebook to show a login prompt to the user. Or can I detect the error and tell the user that he's no longer logged in on Facebook?
Just recall getLoginStatus BUT forcing a roundtrip to Facebook. Look following code:
FB.getLoginStatus(function(response) {
// some code
}, true);
Look the last parameter set to true to force the roundtrip.
From JS SDK documentation:
To improve the performance of your application, not every call to
check the status of the user will result in request to Facebook's
servers. Where possible, the response is cached. The first time in the
current browser session that FB.getLoginStatus is called, or the JS
SDK is init'd with status: true, the response object will be cached by
the SDK. Subsequent calls to FB.getLoginStatus will return data from
this cached response.
This can cause problems where the user has logged into (or out of)
Facebook since the last full session lookup, or if the user has
removed your application in their account settings.
To get around this, you call FB.getLoginStatus with the second
parameter set to true to force a roundtrip to Facebook - effectively
refreshing the cache of the response object.
(http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/)
What you could try and use is the FB.getLoginStatus where if the user is connected this would allow them to complete the wall post.
If they aren't connected then call the FB.login method before they can post on the wall.
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// the user is logged in and has authenticated your
// app, and response.authResponse supplies
// the user's ID, a valid access token, a signed
// request, and the time the access token
// and signed request each expire
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
} else {
// the user isn't logged in to Facebook.
}
});
http://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/
There are also the events for login and logout that you can watch for and do something with those responses.
FB.Event.subscribe('auth.login', function(response) {
// do something with response
});
FB.Event.subscribe('auth.logout', function(response) {
// do something with response
});
http://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/

Categories