以编程方式获得一个访问令牌使用Facebook的图形API [英] Programmatically getting an access token for using the Facebook Graph API

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

问题描述

我试图把一个bash或者python脚本与Facebook的图形API的发挥。使用API​​看起来很简单,但我无法在我的bash脚本设置卷曲打电话给授权和的access_token。有没有人有一个工作的例子吗?

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?

推荐答案

总比不做好迟到,也许其他人搜索,将找到它。我得到了它与Python 2.6在MacBook上工作。

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

这需要你有


  • 安装了Python Facebook的模块:<一href=\"https://github.com/pythonforfacebook/facebook-sdk\">https://github.com/pythonforfacebook/facebook-sdk,

  • 一个实际的Facebook应用程序设置

  • ,并要张贴的个人资料,必须授予适当的权限,允许所有不同的东西,喜欢读书和写作。

您可以阅读有关Facebook的开发者文档中的身份验证的东西。见<一href=\"https://developers.facebook.com/docs/authentication/\">https://developers.facebook.com/docs/authentication/了解详情。

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

本博客文章也可以帮助解决这个:<一href=\"http://blog.theunical.com/facebook-integration/5-steps-to-publish-on-a-facebook-wall-using-php/\">http://blog.theunical.com/facebook-integration/5-steps-to-publish-on-a-facebook-wall-using-php/

This blog post might also help with this: 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

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

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

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

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