0

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.

4
  • so what is the problem? did you debug the code? what is the exact api response? Commented Feb 18, 2020 at 16:26
  • i am getting a error that says,"GraphAPIError: (#33) This object does not exist or does not support this action" Commented Feb 18, 2020 at 16:40
  • are you using a user token and does it include all the neccessary permissions? Commented Feb 18, 2020 at 17:46
  • yes it is a user token and it has all the permissions Commented Feb 18, 2020 at 19:40

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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"],"\n\n")
            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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.