Facebook:如何以编程方式检索访问令牌? [英] Facebook: How to retrieve an access token programmatically?

查看:47
本文介绍了Facebook:如何以编程方式检索访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在自己的自己 Facebook页面上发布一条消息;并且我需要以编程方式(在我的情况下使用Python).我使用这段代码(在Python中)设法完成了这一部分:

I need to post a message on my own Facebook page; and I need to do it programmatically (in my case using Python). I managed to do this part using this code (in Python):

import urllib, urllib2

access_token='XXXX'
fb_page_id='YYYY' # my page ID

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

已正确返回FB页面上生成的帖子的ID:

The ID of the generated post on the FB page is correctly returned:

In [11]: response.readlines()
Out[11]: ['{"id":"135386143198208_461964357207050"}']

问题:

为了生成 access_token 并进行上述API请求,我必须手动遵循以下三个详细步骤

In order to generate the access_token and make the API request above I had to manually follow the three steps detailed here.

但是实际上,这种手动过程是不可接受的,因为我需要从Cron作业中运行此任务.因此,我需要对其进行自动化,因为Facebook中的 access_token 是临时的.IE.每次运行此脚本时,我都需要获取访问令牌.该怎么做?

But in practice this manual process is unacceptable as I need to run this task from a cron job. Hence I need to automate it because access_token in Facebook is temporary. I.e. I need to get an access token each time I run this script. How to do that?

只要您传达所涉及的步骤,就可以在答案中随意使用任何脚本工具(curl,JavaScript,Java,PHP).请注意,我需要使用任何服务器端语言(Python/Ruby/PHP)来完成此操作.

Feel free to use any scripting tool in your answer (curl, JavaScript, Java, PHP) as long you communicate the steps involved. Note that I need to do this using any server-side language (Python/Ruby/PHP).

推荐答案

如果您扩展了(用户)访问令牌,则可以请求实际上根本不会过期的(页面)访问令牌.

If you extend your (User) access token, you can then request a (Page) access token which does not in fact expire at all.

请参阅以下文档的扩展页面访问令牌"部分: https://developers.facebook.com/docs/howtos/login/extending-tokens/

See the "Extending Page access tokens" section of the following document: https://developers.facebook.com/docs/howtos/login/extending-tokens/

这篇关于Facebook:如何以编程方式检索访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆