以编程方式获取使用Facebook Graph API的访问令牌

我正试图把bash或python脚本放在一起来玩facebookgraphicsAPI。 使用API​​看起来很简单,但是在我的bash脚本中设置curl调用authorize和access_token时遇到了麻烦。 有没有人有一个工作的例子?

迟到比从未好,也许其他寻找的人会发现它。 我在MacBook上使用了Python 2.6。

这需要你有

  • 安装了Python的facebook模块: https : //github.com/pythonforfacebook/facebook-sdk ,
  • 一个真正的Facebook应用程序设置
  • 和你想发布的configuration文件必须已经授予适当的权限,以允许所有不同的东西,如阅读和写作。

您可以阅读Facebook开发人员文档中的身份validation内容。 有关详细信息,请参阅https://developers.facebook.com/docs/authentication/

这个博客文章也可能有助于这个: http : //blog.theunical.com/facebook-integration/5-steps-to-publish-on-a-facebook-wall-using-php/

开始:

#!/usr/bin/python # coding: utf-8 import facebook import urllib import urlparse import subprocess import warnings # Hide deprecation warnings. The facebook module isn't that up-to-date (facebook.GraphAPIError). warnings.filterwarnings('ignore', category=DeprecationWarning) # Parameters of your app and the id of the profile you want to mess with. FACEBOOK_APP_ID = 'XXXXXXXXXXXXXXX' FACEBOOK_APP_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' FACEBOOK_PROFILE_ID = 'XXXXXX' # Trying to get an access token. Very awkward. oauth_args = dict(client_id = FACEBOOK_APP_ID, client_secret = FACEBOOK_APP_SECRET, grant_type = 'client_credentials') oauth_curl_cmd = ['curl', 'https://graph.facebook.com/oauth/access_token?' + urllib.urlencode(oauth_args)] oauth_response = subprocess.Popen(oauth_curl_cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()[0] try: oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0] except KeyError: print('Unable to grab an access token!') exit() facebook_graph = facebook.GraphAPI(oauth_access_token) # Try to post something on the wall. try: fb_response = facebook_graph.put_wall_post('Hello from Python', \ profile_id = FACEBOOK_PROFILE_ID) print fb_response except facebook.GraphAPIError as e: print 'Something went wrong:', e.type, e.message 

错误检查获得令牌可能会更好,但你得到了做什么的想法。

在这里,你尽可能简单地去。 不需要任何第三方SDK等

确保安装了Python“请求”模块

 import requests def get_fb_token(app_id, app_secret): payload = {'grant_type': 'client_credentials', 'client_id': app_id, 'client_secret': app_secret} file = requests.post('https://graph.facebook.com/oauth/access_token?', params = payload) #print file.text #to test what the FB api responded with result = file.text.split("=")[1] #print file.text #to test the TOKEN return result 

简单! 只需使用Facebook的SDK 。

get_app_access_token(app_id,app_secret) – 正是你以后;-)

您首先需要设置一个应用程序 。 然后,下面将吐出一个访问令牌给你的应用程序ID和秘密:

 > curl -F type=client_cred -F client_id=[...] -F client_secret=[...] https://graph.facebook.com/oauth/access_token 

由于Web浏览器需要参与实际的授权,因此不存在“独立脚本”这样的事情。 如果您只是在玩API,或者正在编写一个脚本来自动化一些东西,并且希望自己的access_token没有过期,那么您可以在这里获取一个: http : //fbrell.com/auth/offline-access -token

有一种方法可以做到这一点,我find了它,但这需要很多工作,并且要求你100%地浏览一下浏览器(你可能会违反服务条款)

对不起,我不能提供所有的细节,但它的要点:

  1. 假设你有一个Facebook帐户的用户名/密码,去curloauth / authenticate …页面。 提取“Set-Cookie”头文件中返回的任何cookie,然后按照任何“位置”头文件(沿途编译cookie)。
  2. 抓取login表单,保存所有字段,并提交它(设置referer和内容types标题,并插入您的电子邮件/传递)从(1)需要相同的cookie集合
  3. 与(2)相同,但是现在您需要发布(2)提交后获得的批准表单,并将表单获取后的thr URL设置为Referer头部。
  4. 请遵循redirect,直到它将您发送回您的网站,并从该URL中获取“code”参数
  5. 在oauth端点上交换access_token的代码

主要的陷阱是cookiepipe理和redirect。 基本上,你必须模仿浏览器100%。 我认为这是骇人听闻,但有一种方法,这真的很难!

这是Python代码 。 尝试在命令行上运行这些示例中的一些,它们对我来说工作正常。 另见 – http://www.pythonforfacebook.com/