user972616
user972616

Reputation: 1404

How to correctly make a POST call to Facebook Graph API in django

I am trying to create an Facebook Event from my local application and I have a form where a user enters the event information. According to this, I have to make a post call to https://graph.facebook.com/USERID/events

To do so I've used urlopen as such

form_fields = {
           "access_token": request.user.get_profile().access_token,
           "name" : "name",
           "start_time" : "01/01/2012"
}
urllib2.urlopen(update_url,urllib.urlencode(form_fields))

Running the code I get

HTTP Error 400: Bad Request

and after debugging the program the variable values are

update_url = str: https://graph.facebook.com/XXXXXXXX/events

form_fields = dict: {'access_token': u'XXXXXX', 'start_time': datetime.datetime(2012, 1, 6, 0, 0), 'location': 'someplace', 'name': u'ab'}

The update_url seems to be correct and I'm guessing the problem is with form_fields. So how should I give these fields to Facebook Graph API?

    Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Akshay\workspace\example\example\planpouchproto\views.py" in createPouch
  20.             graph.post('%s/events' %request.user.get_profile().facebook_id, **{"access_token": request.user.get_profile().access_token,"name" : cd['event_name'],"start_time" : "01/01/2012"})
File "C:\Users\Akshay\workspace\example\example\facepy\graph_api.py" in post
  65.             retry = retry
File "C:\Users\Akshay\workspace\example\example\facepy\graph_api.py" in _query
  240.                 return load(method, url, data)[0]
File "C:\Users\Akshay\workspace\example\example\facepy\graph_api.py" in load
  196.             result = self._parse(response.content)
File "C:\Users\Akshay\workspace\example\example\facepy\graph_api.py" in _parse
  282.                     error.get('code', None)

Exception Type: OAuthError at /pouch/
Exception Value: 

Upvotes: 1

Views: 858

Answers (1)

Anton C
Anton C

Reputation: 86

Facepy makes the interaction with Facebook simpler, give it a try.

Posting an event to the graph would be something like

graph.post('USERID/events', form_fields)

To debug, you can test if authentication works by posting a comment:

graph = GraphAPI(request.user.get_profile().access_token)
graph.post(path="me/feed", message="hello FB", caption="hello FB", description="hello FB")

Then you can narrow down your problem.

Upvotes: 1

Related Questions