使用Python脚本的POST请求进行Facebook FQL查询

时间:2013-06-02 12:23:31

标签: python http facebook-graph-api facebook-fql

我正在尝试使用以下代码进行FQL查询:

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']

使用POST请求并不是文档中记录最好的,我无法让它工作。使用方法下指定的“fql.query”或“fql”(取自此处的Javascript特定示例:How can I execute a FQL query with Facebook Graph API),我得到响应:

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

当然,这不是文档中的内容。如果没有这种方法规范,我会回来:

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

文档中也未涵盖哪些内容。我不能在这里使用get请求(这是微不足道的),因为我正在做一个相当大的查询,目前不会溢出get请求限制,但很可能在不久的将来。

感谢您在解决此问题方面提供的任何帮助。

编辑:请注意我正在提出要求:

https://graph.facebook.com

2 个答案:

答案 0 :(得分:1)

首先,您要访问哪个网址?我的意思是,为什么你需要FQL的POST请求? FQL用于获取数据,而不是用于发布。

根据文档(https://developers.facebook.com/docs/technical-guides/fql/),您的请求应如下:

https://graph.facebook.com/fql?q=QUERY&access_token=TOKEN - 其中QUERY - 是您对FQL,TOKEN的您的urlencoded查询 - 您的有效访问令牌。

答案 1 :(得分:1)

您需要做的就是了解您的请求是如何构建的,如果您理解了这一点,那么错误将对您更有意义。

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

如果没有运行它,我知道请求看起来像

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

这不是method/fql.query的fql.method,如您所提供的文档https://developers.facebook.com/docs/reference/api/batch/中的相对网址所示。

删除规范(我不知道你为什么要这样做)显然会导致一个未知的错误,因为这是你正在做的请求

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

正确的请求将是

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

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

我不完全确定批处理使用哪个端点允许HTTP POST到方法/ fql.query,所以除非你实际上是在批处理请求,否则我不会依赖它。

最后使用fql.query可能不是最好的方法,因为它正在逐步推荐。

我仍然不确定您的查询如何超过GET请求限制。考虑重新评估如何将查询结构化为多查询或批处理。