Using FB.api to query events and friends events in Javascript

409 Views Asked by At

I am writing a small app that after logging a user into Facebook, runs a query and displays the result in the console, and I am running into problems:

First, I do the login-thing:

FB.Event.subscribe('auth.authResponseChange', function(response) {
        if (response.status === 'connected') {
            var accesstoken=response;
            getSomeEvents(accesstoken);
        } else if (response.status === 'not_authorized') {
            FB.login(function(response){
            }, {scope: 'email,user_likes, user_events, friends_events'});

        } else {
            FB.login(function(response){
            }, {scope: 'email,user_likes, user_events, friends_events'});
        }
    });
};

Then, I try to query FB, with the Graph API. Below, I am simplifying the query to make my problem clearer

function getSomeEvents(accesstoken){
  var query2=encodeURI("SELECT name, eid, venue, location FROM event WHERE eid=364689750343572 & access_token="+accesstoken.authResponse.accessToken);

    //Method 1: Querying with the graph API
    FB.api('/fql?q='+query2, function(events) {
        console.log('In Method 1 - Updated Graph API - event-name is ' + events.name + '.');
    });
   }

The chrome debugger gives me this: "Your statement is not indexable. The WHERE clause must contain an indexable column. Such columns are marked with * in the tables linked from..."

However, EID is indexable according to this: https://developers.facebook.com/docs/reference/fql/event

Moreover, if I replace the query with what I am actually trying to do:

var query2=encodeURI("SELECT name, eid, venue, location FROM event WHERE eid in ( SELECT eid FROM event_member WHERE uid in (SELECT uid2 FROM friend WHERE uid1 = me()) ) limit 100 & access_token="+accesstoken.authResponse.accessToken);

I get this in the chrome debugger: "(#601) Parser error: unexpected end of query."; type: OAuthException

Both queries work in Facebooks Graph API console, and return data when typed directly into the browser as GET-requests (http://graph.facebook.com/fql?q=).

Any ideas as to what I am doing wrong?

1

There are 1 best solutions below

0
On

You do not need the accesstoken to get the details of an event.

I don't know why you seem to be getting this problem, I assume its an encoding error. Anyway this works:

var query2="SELECT name, eid, venue, location FROM event WHERE eid=364689750343572";

FB.api('/fql',{ q: query2}, function(response) {

        console.log(response.data)
        console.log('In Method 2 - Updated Graph API - event-name is ' + response.data[0].name + '.');
    });     
}

Remember the results are returned under response.data, and those results are an array.

PS you can get event info directly without using FQL:

FB.api('/364689750343572/', function(response) {            
console.log('In Method 1 - Updated Graph API - event-name is ' + response.name + '.');
});        
}

Source: https://developers.facebook.com/docs/reference/javascript/FB.api/