Facebook FQL Query with POST Request from a Python Script

I'm trying to make an FQL query with the following code:

def get_postData(to_post, access_token):
    postData = {}
    postData["method"] = "fql.query"
    postData["query"] = to_post
    postData["access_token"] = access_token
    return postData

def make_request(url, to_post, access_token):
    postData = get_postData(to_post, access_token)
    return requests.post(url, data = postData).json()[u'data']

Using POST requests is not the best documented in the docs, and I'm unable to get this to work. With either "fql.query" or "fql" specified under method (taken from the Javascript specific example here: How can I execute a FQL query with Facebook Graph API), I get the response:

{u'error': {u'message': u'Unsupported method, fql.query', u'code': 100, u'type': u'GraphMethodException'}}

Which is, of course, not covered in the docs. Without that method specification, I get back:

{u'error': {u'message': u'Unsupported post request.', u'code': 100, u'type': u'GraphMethodException'}}

Which is also not covered in the docs. I'm not able to use a get request here (which is trivial), as I'm making a rather large query that at the moment doesn't overflow the get request limits but very well could in the near future.

Thanks for any help you may be able to give with regards to solving this problem.

EDIT: Should note I'm making the request to:

https://graph.facebook.com

Upvotes: 0

Views: 1495

Answers (2)

phwd
phwd

Reputation: 19995

All you need to do is understand how your requests are built, if you understand that, then the errors will make more sense to you.

postData = {}
postData["method"] = "fql.query"
postData["query"] = to_post
postData["access_token"] = access_token
requests.post(url, data = postData).json()[u'data']

Without even running this, I know the request looks like

POST https://graph.facebook.com/?method=fql.query&query=THE_QUERY&access_token=THE_TOKEN

Which is not the fql.method of method/fql.query as shown as the relative url in the doc https://developers.facebook.com/docs/reference/api/batch/ you presented.

Removing the specification (I don't know why you want to do this) will obviously result in an unknown error since this is now the request you are making

POST https://graph.facebook.com/?query=THE_QUERY&access_token=THE_TOKEN

The correct request will be

GET https://api-read.facebook.com/restserver.php?method=fql.query&query=THE_QUERY&access_token=THE_TOKEN

or

GET https://api.facebook.com/method/fql.query&query=THE_QUERY&access_token=THE_TOKEN

I'm not entire sure what endpoint the batch uses that allows an HTTP POST to method/fql.query so I wouldn't rely on it unless you are actually doing batch requests.

In the end using fql.query may not be the best way to go since it's on its way to deprecation.

I'm still unsure how your query could be so long that it exceeds the GET request limit. Consider re-evaluating how you structure your query as a multi-query or in batch.

Upvotes: 1

Sergey Pronin
Sergey Pronin

Reputation: 193

First of all, what URL are you trying to access? I mean, why do you need POST request for FQL? FQL is for fetching data, not for posting.

According to docs (https://developers.facebook.com/docs/technical-guides/fql/) your request should look like that:

https://graph.facebook.com/fql?q=QUERY&access_token=TOKEN - where QUERY - is your urlencoded query to FQL, TOKEN - your valid access token.

Upvotes: 1

Related Questions