0

i am trying to post a message into my wall using facebook graph api. I have my access_token. I have tried in my browser using following URL.

https://graph.facebook.com/me/feed?message="Hii friends"&access_token=xxxxxxxxxx

But message is not posted. So i couldn't solve this problem. Finally i want to use this URL inside urllib2.urlopen()

Please Help

user3136030
  • 384
  • 4
  • 9
  • 19

3 Answers3

4

I recently got posting to facebook to work like so:

import requests

face_token = 'your_token'
post = 'Your Post wowzers'
post.replace(' ', '+')
requests.post("https://graph.facebook.com/me/feed/?message=" + post + "&access_token=" + face_token)
Jebsky
  • 56
  • 3
  • Shoul not do post.replace, because you have to encode all special characters in the URL query string. You can do `import urllib.parse` and then `qs = urllib.parse.urlencode({"message": post, "access_token": face_token})` and finally `requests.post("https://graph.facebook.com/me/feed/?%s" % qs)` – fero Jun 02 '18 at 21:49
4

You can do it with the graph api like this:

import facebook
def main():
    graph = facebook.GraphAPI(access_token='your_user_access_token', version='2.8')
    #if version 2.8 show error use 2.6
    attachment =  {
        'name': 'Link name'
        'link': 'https://www.example.com/',
        'caption': 'Check out this example',
        'description': 'This is a longer description of the attachment',
        'picture': 'https://www.example.com/thumbnail.jpg'
    }

    graph.put_wall_post(message='Check this out...', attachment=attachment, profile_id='your_page_id')

if __name__ == "__main__":
    main()

If you leave the profile_id blank, it will default to your profile. In the attachment dictionary, you can leave the extra fields that you don't need. First you need to install fackbook-sdk:

pip install facebook-sdk
akimul
  • 319
  • 1
  • 5
  • 1
    following is the github link of facebook-sdk: there is example of attachment usage. [link](https://github.com/eleith/python-sdk/blob/master/src/facebook.py) – akimul Oct 27 '17 at 17:13
2

This will most likely not work in the browser. Check out some other threads on stackoverflow for this:

Also, check out google, there are many tutorials and frameworks for using the Facebook API with Python.

Community
  • 1
  • 1
andyrandy
  • 72,880
  • 8
  • 113
  • 130