mobolic / facebook-sdk

Python SDK for Facebook's Graph API
https://facebook-sdk.readthedocs.io/
Apache License 2.0
2.74k stars 950 forks source link

Maintype was not text, image, or querystring #286

Closed louisguitton closed 8 years ago

louisguitton commented 8 years ago

When trying to get posts of a page, you use this endopoint: pagename?fields=posts.limit(10) Most of the posts are of this type:

      {
        "message": "Elle va faire battre votre ❤️ ! #EMEHARI #PlageDeLaBastille",
        "created_time": "2016-05-22T12:54:14+0000",
        "id": "103666283041018_1048666508540986"
      }

But some have one field more:

{
  "created_time": "2016-03-13T16:00:44+0000",
  "message": "E-MEHARI \"styled by Courrèges\" a électrisé le Salon de Genève !",
  "story": "Citroën added 4 new photos.",
  "id": "103666283041018_994707227270248"
}

When trying to retrieve Citroen.France 's last 50 posts, I get this error because of this field:

Traceback (most recent call last):
  File "myproject/myfacebookapiclient.py", line 21, in get_page_posts
    page_posts = graph.get_object(page_id, **args)
  File "myproject/src/facebook-sdk/facebook/__init__.py", line 106, in get_object
    return self.request(self.version + "/" + id, args)
  File "myproject/src/facebook-sdk/facebook/__init__.py", line 272, in request
    raise GraphAPIError('Maintype was not text, image, or querystring')
facebook.GraphAPIError: Maintype was not text, image, or querystring

I printed the header, and it looks like this:

{  
   "Content-Length":"108",
   "X-FB-Trace-ID":"H8Vc/+RKGp2",
   "Expires":"Sat, 01 Jan 2000 00:00:00 GMT",
   "X-FB-Rev":"2349332",
   "Connection":"keep-alive",
   "Pragma":"no-cache",
   "Cache-Control":"private, no-cache, no-store, must-revalidate",
   "Date":"Mon, 23 May 2016 09:29:08 GMT",
   "Facebook-API-Version":"v2.6",
   "Access-Control-Allow-Origin":"*",
   "Content-Type":"text/html",
   "X-FB-Debug":"R79ZCTpQx1n51dpXygxmEBADxwEWZxLbrEp8TcxDBLaRGYIPLOfwg9NmFMmN/oT4U6ixSauuDmV8MHnshQbr6Q=="
}

It looks like it's a new feature from Facebook, that the if/elif/else loop of the init.py file is not handling the way the API explorer is for example

martey commented 8 years ago

I think this issue is a duplicate of #281 - the "Maintype was not text, image, or querystring" message is hiding the real error.

louisguitton commented 8 years ago

Indeed, thanks for the really quick reply! what I did is add these lines on line 270:

        elif "text/html" in headers['content-type']:
            result = response.json()

It's just temporary fixing but it does the trick for now.