Marcel
Marcel

Reputation: 11

Facebook graph API fail to load resource bad request 400

I have the following code on my controller.js and works when I request from Facebook Graph 2.4

request.open("GET","https://graph.facebook.com/v2.4/katyperry/posts?"+access_token,true);

However this only returns few fields: message, story, created_time, and id.

I need some additional more fields: message, picture, likes, created_time, link, type, comments and object_id.

So I tried the following:

request.open("GET","https://graph.facebook.com/v2.4/katyperry?fields=posts.limit(2){message,picture,likes,created_time,link,type,comments,object_id}"+access_token,true);

The last one sent me the error:

Failed to load resource: the server responded with a status of 400 (Bad Request)

I tested both options the request on the Graph API Explorer and it worked properly. How can I get those extra fields? what am I missing?

I am adding the precedent code to show how access_token is builded:

var appID ="138429819833219";
var appSecret ="dada0a4d7f7717b0d37fd637d9e1522a";

var accessTokenRq = makeHttpRequest();
var httpString = 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id='+appID+'&client_secret='+appSecret;

accessTokenRq.open("GET",httpString,true);
accessTokenRq.send(null);

var access_token;

accessTokenRq.onreadystatechange = function() {

  if (accessTokenRq.readyState == 4) {
    access_token = accessTokenRq.responseText;
    //alert("Hat geklappt - Trabajó :)");

    var request = makeHttpRequest();
    
// the following request works
    request.open("GET","https://graph.facebook.com/v2.2/katyperry/posts?"+access_token,true);


// next one don't - I checked inspector and access_token contains: 138429819833219|CewVrYOd86ctJ0HTP2X0XAS9m4o
    request.open("GET","https://graph.facebook.com/v2.4/katyperry?fields=posts.limit(2){message,picture,likes,created_time,link,type,comments,object_id}"+access_token,true);

Upvotes: 0

Views: 2043

Answers (2)

Marcel
Marcel

Reputation: 11

The error it was syntax, the right code as follow:

request.open("GET","https://graph.facebook.com/v2.4/katyperry/posts?limit=2&fields=message,picture,likes,created_time,link,type,comments,object_id&"+access_token,true);

Upvotes: -1

Tobi
Tobi

Reputation: 31479

What is contained in the access_token variable? The request

/katyperry?fields=posts.limit(2){message,picture,likes,created_time,link,type,comments,object_id}

works in the Graph API Explorer, so I guess your access_token variable only contains the actual access token, and not the parameter key/value pair.

This

request.open("GET","https://graph.facebook.com/v2.4/katyperry?fields=posts.limit(2){message,picture,likes,created_time,link,type,comments,object_id}&access_token="+access_token,true);

should hopefully work then.

Upvotes: 1

Related Questions