获取 Facebook 页面的页面 ID 使用令牌页面。

huangapple go评论71阅读模式
英文:

how to get page id facebook pages using token pages

问题

当我执行这个函数时

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

使用 fastapi 帮助我上传动态时,出现如下错误

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

我根据这里的文档创建了这个程序
https://developers.facebook.com/docs/pages/getting-started/

这是 fastapi 的端点

@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)

使用页面令牌获取页面 ID 以上传到 Facebook 页面的动态。

英文:

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

得分: 0

不要使用此网址,如果您使用页面令牌:https://graph.facebook.com/v17.0/me/accounts?fields=id&access_token=
请使用此网址:
https://graph.facebook.com/me?access_token=
完整的代码如下:

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
英文:

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

huangapple
  • 本文由 发表于 2023年6月18日 17:59:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76499960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定