NGuyen NGuyen - 1 year ago 122
Python Question

Python Facebook SDK: How to get response from request?

I am using the following code to publish photo but I don't know how to get response ID post. How to get it ?

import facebook

graph = facebook.GraphAPI(access_token='mytoken', version='2.7')

graph.put_photo(image=open(r'E:\Facebook\myphoto.jpg', 'rb'), message='Cool'.encode('utf-8'))

Also, Facebook doesn't show photo in wall. It shows "no automatic alt text available". So, how to publish photo correctly ?

Thank you :)

Answer Source

According to the documentation put_photo() should return a decoded JSON object (i.e. a dictionary( containing the ID and the post ID. Try this:

import json

photo = graph.put_photo(image=open(r'E:\Facebook\myphoto.jpg', 'rb'), message='Cool'.encode('utf-8')) 
print('id: {}'.format(photo['id']))
print('photo_id: {}'.format(photo['post_id']))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download