skip to Main Content

How can i get all my facebook posts using python code and facebook graph api.
i have tried using this code:

import json
import facebook


def get_basic_info(token):

    graph = facebook.GraphAPI(token)

    profile = graph.get_object('me',fields='first_name,last_name,location,link,email')  

    print(json.dumps(profile, indent=5))

def get_all_posts(token):
    graph = facebook.GraphAPI(token)
    events = graph.request('type=event&limit=10000')
    print(events)


def main():

    token = "my_token"
    #get_basic_info(token)
    get_all_posts(token)



if __name__ == '__main__':

    main()

I am getting a error that says,
“GraphAPIError: (#33) This object does not exist or does not support this action”.

Seems like all the other stackoverflow questions are very old and does not apply for the newest version of facebook graph API. I am not entirely sure whether u can do this using facebook graph api or not.
if this is not possible using this technique, is there any other way i can get my posts using python?
please note that function get_basic_info() is working perfectly.

2

Answers


  1. Chosen as BEST ANSWER

    I have solved this problem with the help of the first answer by @luschn I was making one more mistake, that is using events for getting all my posts.instead i should have used me/posts in my code. Here is the function that works perfectly in version 6.

    def get_all_posts(graph):
    
        posts = graph.request('/me/posts')
        count=1
        while "paging" in posts: 
            print("length of the dictionary",len(posts))
            print("length of the data part",len(posts['data']))
            for post in posts["data"]:
                print(count,"n")
                if "message" in post:   #because some posts may not have a caption
                    print(post["message"]) 
                print("time :  ",post["created_time"])
                print("id   :",post["id"],"nn")
                count=count+1
    
            posts=requests.get(posts["paging"]["next"]).json()
    
        print("end of posts")
    

    Here, the post["data"] only gives first 25 posts, so I have used posts["paging"]["next"] link to get the next page as long as there is the next page.


  2. I assume you want to get user events: https://developers.facebook.com/docs/graph-api/reference/user/events/

    Be aware:

    This edge is only available to a limited number of approved apps. Unapproved apps querying this edge will receive an empty data set in response. You cannot request access to this edge at this time.

    Either way, the API would not be type=event&limit=10000 but /me/events instead.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search