使用Python自动发布到Facebook页面

发布于 2024-10-18 14:22:53 字数 528 浏览 2 评论 0原文

我想制作一个脚本,让我可以发布到我的 facebook 粉丝页面(我是该页面的管理员)

据我所知,大多数图形 api 示例都是关于用 python 制作 facebook 应用程序并让它们与 python 进行通信,这与我想要的完全不同。

此外,图形 api 需要 oauth 令牌,文档声称它是通过执行以下操作获得的:

https://www.facebook .com/dialog/oauth? client_id=YOUR_APP_ID&redirect_uri=YOUR_URL

我认为这意味着:

a)我必须为此创建一个 facebook 应用程序,我认为没有必要(毕竟它只需要我的标准凭据,并且不会被其他人使用),但没关系。我为此任务创建了一个应用程序。

b) 我需要一个 URL,但我没有,因为它只是一个脚本。

关于我应该在哪里寻找一些信息有什么想法吗?

I want to make a script that lets me post to my facebook fan page (which I'm an admin of)

As far as I've seen, Most graph api examples are about making facebook apps in python and make them communicate with python, which is quite different from what I want.

Also The graph api requires the oauth token, which the documentation claims it's obtained by doing:

https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL

I think this implies:

a) I HAVE to create a facebook app for this, which I didn't think it was necessary (after all it's something that would require only my standard credentials and wouldn't be used by other people), but it is fine. I have an app created for this task.

b) I need an URL, which I don't have, because it's just a script.

Any ideas on where I should look for some info?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鼻尖触碰 2024-10-25 14:22:53

首先,您需要从 facebook 获取您的 Facebook_App_Id 和 Facebook_App_Secret,您将在注册应用程序时获得。

然后添加所需的网址。

redirect_client_url = 'http://your-redirect-url'
access_token_url = 'https://graph.facebook.com/oauth/access_token?client_id='+consumer_key+'&redirect_uri='+red   irect_client_url+'&client_secret='+consumer_secret+'&code='
scope = 'publish_stream,offline_access,user_birthday,email'
authorize_url = 'https://graph.facebook.com/oauth/authorize?client_id='+consumer_key+'&redirect_uri='+redirect_client_url+'&scope='+scope+'&display=touch'
user_info_url = 'https://graph.facebook.com/me?access_token='

您的消费者密钥和消费者秘密分别是 Facebook 应用程序 ID 和 Facebook 应用程序秘密。

您基本上可以按照 Oauth2.0 指南获取 access_token,并将 access_token 和粉丝页面 id 存储在数据库中的某个位置。 https://github.com/simplegeo/python-oauth2 是一个关于如何获取 oauth 令牌。然后,当您尝试发布时,请使用类似这样的内容来使用访问令牌。

post_data = {'access_token':access_token, 'message':'hey this is a test!'}
request_path = str(facebook_id)+'/feed'
post_data = urllib.urlencode(post_data)
response = urllib2.urlopen('https://graph.facebook.com/%s' % request_path, post_data)

这应该适用于发布到用户的墙上。但我确信在 Facebook 粉丝页面上发帖应该是非常相似的。

First You need to get your Facebook_App_Id and Facebook_App_Secret, from facebook which you will get when you register your app.

Then you include the needed urls.

redirect_client_url = 'http://your-redirect-url'
access_token_url = 'https://graph.facebook.com/oauth/access_token?client_id='+consumer_key+'&redirect_uri='+red   irect_client_url+'&client_secret='+consumer_secret+'&code='
scope = 'publish_stream,offline_access,user_birthday,email'
authorize_url = 'https://graph.facebook.com/oauth/authorize?client_id='+consumer_key+'&redirect_uri='+redirect_client_url+'&scope='+scope+'&display=touch'
user_info_url = 'https://graph.facebook.com/me?access_token='

Your consumer key and consumer secret are the Facebook app id and facebook app secret respectively.

You would basically get the access_token following the Oauth2.0 guidelines and store the access_token and fan page idsomewhere in your database. https://github.com/simplegeo/python-oauth2 is a good example on how to get the oauth token. Then when you try to post use the access token by using something like this.

post_data = {'access_token':access_token, 'message':'hey this is a test!'}
request_path = str(facebook_id)+'/feed'
post_data = urllib.urlencode(post_data)
response = urllib2.urlopen('https://graph.facebook.com/%s' % request_path, post_data)

This should work for posting to a user's wall. But i am sure posting to a facebook fan page should be something very similar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文