1

when I execute this function

def cari_id(access_token):
    url = f"https://graph.facebook.com/v17.0/me/accounts?fields=id&access_token={access_token}"
    response = requests.get(url)
    data = response.json()
    
    page_id = data["data"][0]["id"]
    return page_id

using fastapi to help me upload feeds for some reason it always outputs an error like this

page_id = data["data"][0]["id"]
KeyError: 'data'

I created this program based on the documentation here https://developers.facebook.com/docs/pages/getting-started/

here fastapi endpoint

@app.post("/post-to-feed")
async def post_feed(pesan: str, access_token: str= Header(..., description="meta token")):
    page_id = cari_id(access_token)
    response = create_feed(page_id, access_token, pesan)
    return JSONResponse (response)

get page id using page token to upload to feed facebook pages

  • 1
    Please have a look at [this](https://stackoverflow.com/a/76458566/17865804) – Chris Jun 18 '23 at 10:38
  • @Chris I think I set the request url wrong because I used the page token and not the user token I should have used this url = 'https://graph.facebook.com/me?access_token=' – Dimas Surya Jun 19 '23 at 05:17

1 Answers1

0

dont use this url if you use page token: https://graph.facebook.com/v17.0/me/accounts?fields=id&access_token= use this url: https://graph.facebook.com/me?access_token= this full code:

def cari_id(access_token):
token_pages_url = f'https://graph.facebook.com/me?access_token={access_token}'
token_pages = requests.get(token_pages_url)
data = token_pages.json()
page_id = data["id"]
return page_id