python - 如何使用 python 在 facebook 上写帖子

标签 python facebook-graph-api facebook-sdk-4.0

我尝试使用在 google 上找到的示例代码..

import facebook

def main():
   # Fill in the values noted in previous steps here
    cfg = {
    "page_id"      : "XXXXXXXXXXXXXX",  # Step 1
    "access_token" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"   # Step 3
    }

    api = get_api(cfg)
    msg = "Hello, world!"
    status = api.put_wall_post(msg)

def get_api(cfg):
     graph = facebook.GraphAPI(cfg['access_token'])
     # Get page token to post as the page. You can skip 
     # the following if you want to post as yourself. 
     resp = graph.get_object('me/accounts')
     page_access_token = None
    for page in resp['data']:
        if page['id'] == cfg['page_id']:
            page_access_token = page['access_token']
    graph = facebook.GraphAPI(page_access_token)
    return graph

if __name__ == "__main__":
     main()

但是我收到了这个错误:

AssertionError: Write operations require an access token on line status = api.put_wall_post(msg).

有人可以帮我解决这个问题吗?

images

最佳答案

要使用 python 向 facebook 发帖。我们需要 access_token。

graph = facebook.GraphAPI(access_token="XXXXXXXX")
print graph
#to post to your wall
graph.put_object("me", "feed", message="Posting on my wall1!")
#to get your posts/feed
feed = graph.get_connections("me", "feed")
post = feed["data"]
print post
#to put comments for particular post id
graph.put_object(post["id"], "comments", message="First!")

关于python - 如何使用 python 在 facebook 上写帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48579740/

相关文章:

python - 不支持的操作 :not writeable python

facebook - 如何使用FB API查找我的应用程序的Facebook用户列表?

android - 尝试将 facebook Uri 上传到 Firebase 存储时出错

Facebook 对象调试器 - 无法将主机名解析为有效的 IP 地址

python - 如何从现有数据制造 Django ORM QueySet/从纯 SQL 返回 ORM 查询?

python - scipypercentileofscore 的加权版本

python - 并行运行 appium 测试时的视频屏幕录制(需要线程帮助)

swift - 请求 iOS 应用程序 Facebook 应用程序事件的跟踪授权

安卓 Facebook 登录 : How to check login using LoginManager without having LoginButton in XML

iphone - 登录 iPhone 的 Facebook 设置覆盖 FacebookSDK 共享