以编程方式获取访问令牌以使用 Facebook Graph API [英] Programmatically getting an access token for using the Facebook Graph API

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

问题描述

我正在尝试将 bash 或 python 脚本放在一起以与 facebook 图形 API 一起使用.使用 API 看起来很简单,但我在我的 bash 脚本中设置 curl 来调用授权和访问令牌时遇到问题.有没有人有一个可行的例子?

解决方案

更新2018-08-23

由于这仍然得到一些观点和赞成,我只想提一下,现在似乎存在一个维护的 3rd 方 SDK:https://github.com/mobolic/facebook-sdk

<小时>

迟到总比不到好,也许其他搜索者会找到它.我在 MacBook 上使用了 Python 2.6.

这需要你有

  • 安装的 Python facebook 模块:https://github.com/pythonforfacebook/facebook-sdk,
  • 一个实际的 Facebook 应用程序设置
  • 并且您要发布的个人资料必须已授予适当的权限,以允许所有不同的内容(例如阅读和写作).

您可以在 Facebook 开发人员文档中阅读有关身份验证的内容.详情请参阅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# 编码:utf-8导入脸书导入 urllib导入 urlparse导入子流程进口警告# 隐藏弃用警告.facebook 模块不是最新的 (facebook.GraphAPIError).warnings.filterwarnings('ignore', category=DeprecationWarning)# 你的应用的参数和你想要弄乱的配置文件的 id.FACEBOOK_APP_ID = 'XXXXXXXXXXXXXXX'FACEBOOK_APP_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'FACEBOOK_PROFILE_ID = 'XXXXXX'# 尝试获取访问令牌.很别扭.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,标准输出 = subprocess.PIPE,stderr = subprocess.PIPE).communicate()[0]尝试:oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'][0]除了 KeyError:print('无法获取访问令牌!')出口()facebook_graph = facebook.GraphAPI(oauth_access_token)# 尝试在墙上张贴一些东西.尝试:fb_response = facebook_graph.put_wall_post('Hello from Python', profile_id = FACEBOOK_PROFILE_ID)打印 fb_response除了 facebook.GraphAPIError 作为 e:打印 '出现问题:', e.type, e.message

检查获取令牌的错误可能会更好,但您知道该怎么做.

I am trying to put together a bash or python script to play with the facebook graph API. Using the API looks simple, but I'm having trouble setting up curl in my bash script to call authorize and access_token. Does anyone have a working example?

解决方案

Update 2018-08-23

Since this still gets some views and upvotes I just want to mention that by now there seems to exist a maintained 3rd party SDK: https://github.com/mobolic/facebook-sdk


Better late than never, maybe others searching for that will find it. I got it working with Python 2.6 on a MacBook.

This requires you to have

  • the Python facebook module installed: https://github.com/pythonforfacebook/facebook-sdk,
  • an actual Facebook app set up
  • and the profile you want to post to must have granted proper permissions to allow all the different stuff like reading and writing.

You can read about the authentication stuff in the Facebook developer documentation. See https://developers.facebook.com/docs/authentication/ for details.

This blog post might also help with this: http://blog.theunical.com/facebook-integration/5-steps-to-publish-on-a-facebook-wall-using-php/

Here goes:

#!/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

Error checking on getting the token might be better but you get the idea of what to do.

这篇关于以编程方式获取访问令牌以使用 Facebook Graph API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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