【问题标题】:Python how to do facebook wall post using graph api?Python如何使用graph api做facebook墙贴?
【发布时间】:2014-07-28 06:44:34
【问题描述】:

我正在尝试使用 facebook graph api 将消息发布到我的墙上。我有我的 access_token。我已经尝试在我的浏览器中使用以下 URL。

https://graph.facebook.com/me/feed?message="Hii friends"&access_token=xxxxxxxxxx

但是消息没有发布。所以我无法解决这个问题。最后我想在 urllib2.urlopen() 中使用这个 URL

请帮忙

【问题讨论】:

  • 您需要进行 POST 调用。你想要做的是 GET。

标签: python-2.7 facebook-graph-api


【解决方案1】:

我最近在 facebook 上发布了这样的工作:

import requests

face_token = 'your_token'
post = 'Your Post wowzers'
post.replace(' ', '+')
requests.post("https://graph.facebook.com/me/feed/?message=" + post + "&access_token=" + face_token)

【讨论】:

  • 不应执行 post.replace,因为您必须对 URL 查询字符串中的所有特殊字符进行编码。你可以先import urllib.parse 然后qs = urllib.parse.urlencode({"message": post, "access_token": face_token}) 最后requests.post("https://graph.facebook.com/me/feed/?%s" % qs)
【解决方案2】:

您可以像这样使用图形 api 来做到这一点:

import facebook
def main():
    graph = facebook.GraphAPI(access_token='your_user_access_token', version='2.8')
    #if version 2.8 show error use 2.6
    attachment =  {
        'name': 'Link name'
        'link': 'https://www.example.com/',
        'caption': 'Check out this example',
        'description': 'This is a longer description of the attachment',
        'picture': 'https://www.example.com/thumbnail.jpg'
    }

    graph.put_wall_post(message='Check this out...', attachment=attachment, profile_id='your_page_id')

if __name__ == "__main__":
    main()

如果您将 profile_id 留空,它将默认为您的个人资料。在附件字典中,您可以留下您不需要的额外字段。首先你需要安装 fackbook-sdk:

pip install facebook-sdk

【讨论】:

  • 以下是facebook-sdk的github链接:有附件使用示例。 link
【解决方案3】:

这很可能在浏览器中不起作用。请查看 stackoverflow 上的其他一些线程:

另外,请查看 google,有许多教程和框架可用于将 Facebook API 与 Python 结合使用。

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 2013-03-15
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多