[Python] using python facebook api (noob friendly)

griimnak

You're a slave to the money then you die
Jul 20, 2013
955
794
In this tutorial i'll be showing you how to use facebook's graphAPI in python.

Pre-requisites:
  • python 2.7:
Start by downloading the api from facebook:
unzip it to your desktop, then launch a terminal session and cd into your facebook api directory
Hxnrc8o.jpg


then run the python instal script
Code:
python setup.py install
2Kdm83V.jpg

XzDWq1S.jpg


Now we can delete that facebook folder and start our script.
BuVPO8w.png


create a new folder and use your desired text editor and save a .py file into it.
b56Wlsr.png


Now we can start our script, we'll make an auto-liking script for starters.
MDFRL3i.png


we begin by importing our api library that we installed earlier, you will see this alot in python as you need certain librarys to do certain functions that are not availible in python by default. Think of these librarys as python code plugins
Code:
import facebook

now we need to generate a token to allow our desired facebook account to use this api and perform tasks,
NlaZtxA.png


then copy your access token and place it in our token variable
Code:
token = "your_token_here"

now we can specify the page that we want to auto like, and specify what object we are going to be touching, in this case the posts.
Code:
profile = graph.get_object("sharefast")
posts = graph.get_connections(profile['id'], "posts")

Now we make a simple loop, like each post that is outputed from our graph.get_connections query.
Code:
for post in posts['data']:
    try:
            graph.put_object(post['id'], "likes")
            print "liking topic: " + post['message']
    except:
            continue

Here's the whole script:
Code:
import facebook

token = "token here"

graph = facebook.GraphAPI(token)
profile = graph.get_object("sharefast")
posts = graph.get_connections(profile['id'], "posts")

for post in posts['data']:
    try:
            graph.put_object(post['id'], "likes")
            print "liking topic: " + post['message']
    except:
            continue

now open a new terminal session again and run the file with this command:
Code:
python yourfile.py
0UmuW3v.png

Lj3Niar.png


that's as simple as it gets.
check out theses references to do more cool stuff with this api:



cheers
 

Users who are viewing this thread

Top