Saturday 27 August 2016

An Example of facebooks unofficial python sdk usage

step 1) Install unofficial sdk:

You can get facebook-sdk from here!

Python Facebook-sdk: https://facebook-sdk.readthedocs.io/

Installing from Git
For the newest features, you should install the SDK directly from Git.
Run the following command:

pip install -e git+https://github.com/mobolic/facebook-sdk.git#egg=facebook-sdk
 

step 2) Concepts:      

       Facebook uses something called as graph which is a data structure used to model facebook's data where every person, comment , Post , Share everything we see is an object having its unique id and multiple connections to loads of other objects.
          This helps to model social connections like friendlist , hobbies, likes and shares . We can use this sdk in python to communicate to the graph api and carry out tasks needed. 

step 4) Access token generation:
        Facebook account authentication is done using unique access tokens.
 goto https://developers.facebook.com/ and login to your account click on tools and support 
and Graph API Explorer finally click on get access token and select permissions for below example we will need v2.1 with read_stream and Publish permission.


step 3) Code:

This is an simple example of automatic liker


import facebook
import requests
if __name__ == '__main__':
    token = '***Paste Your Access Token Here***'
    graph = facebook.GraphAPI(token)
    profile = graph.get_object("me")
    feed = graph.get_connections(id="me", connection_name="home")
    toberemoved=[]
    posts=feed
    for i in range(4):
        try:
            # Perform some action on each post in the collection we receive from
            # Facebook.
            # Attempt to make a request to the next page of data, if it exists.
           
            posts = requests.get(posts['paging']['next']).json()
            feed['data'].extend(posts['data'])
        except KeyError:
            # When there are no more pages (['paging']['next']), break from the
            # loop and end the script.
            break
    print 'aquired {}'.format(len(feed['data']))   
    #:::::::::::::::like all the posts::::::::::
    for post in feed['data'] :
        graph.put_like(post['id'])

       
       
   

 Tags: Facebook-sdk, python, Hack facebook, facebook program, facebook automate