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

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

问题描述

我正在尝试使用一个bash或者python脚本来播放facebook图形API。使用API​​看起来很简单,但是我无法在我的bash脚本中设置curl来调用authorize和access_token。有没有人有一个工作的例子?

解决方案

更好的晚于从未,也许其他人搜索会找到它。我在MacBook上使用Python 2.6。



这需要你有





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

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



这里:

 #! / usr / bin / python 
#code:utf-8

import facebook
import urllib
import urlparse
import subprocess
import warnings

#隐藏废弃警告。 Facebook模块不是最新的(facebook.GraphAPIError)。
warnings.filterwarnings('ignore',category = DeprecationWarning)


#您的应用程序的参数和您想要混淆的配置文件的ID。
FACEBOOK_APP_ID ='XXXXXXXXXXXXXXX'
FACEBOOK_APP_SECRET ='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
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,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE ).communicate()[0]

try:
oauth_access_token = urlparse.parse_qs(str(oauth_response))['access_token'] [0]
除了KeyError:
print('无法抓取访问令牌!')
exit()

facebook_graph = facebook.GraphAPI(oauth_access_token)


#尝试在墙上张贴东西。
尝试:
fb_response = facebook_graph.put_wall_post(你好,从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?

解决方案

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天全站免登陆