如何使用Python& GraphAPI? [英] How do I update FB Status using Python & GraphAPI?

查看:115
本文介绍了如何使用Python& GraphAPI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前已经提到了这个问题,但是许多解决方案已被弃用,并且GraphAPI的要求似乎使许多解决方案无关紧要。
我已经玩过fbpy,facebook,oauth,oauth2包,并通过他们的例子,但仍然无法弄清楚如何使它的工作。我不相信任何代码和我一直在使用的软件包,并且想知道是否有任何有明确的解决方案,他们知道将工作。谢谢。

解决方案

首先你需要做的是理解

*发布Python中的照片 - 基本酱: http://philippeharewood.com/facebook/publish-a-facebook-photo-in-python-the-basic-sauce/

* Facebook和Python - 基本酱: http://philippeharewood.com/facebook / facebook-and-python-the-basic-sauce /


This question has been asked before, but many of the solutions have been deprecated and the requirement of GraphAPI seems to have rendered many solutions irrelevant. I have fiddled around with the fbpy, facebook, oauth, oauth2 packages, and have looked through their examples, but still cannot figure out how to get it working. I have no trust in any of the code nor the packages I have been using, and am wondering if anyone has any definitive solutions that they know will work. Thanks.

解决方案

First you need to do is understand login flows. You should understand if you easily want to switch through the different Facebook libraries. Therefore it can have code that is very verbose to code that is very simple based on implementation.

The next thing is that there are different ways to implement handling OAuth and different ways to display and launch your web app in Python. There is no way to authorize without hitting a browser. Otherwise you would have to keep copy pasting the access_token to the code.

Let's say you chose web.py to handle your web app presentation and requests.py to handle the Graph API HTTP calls.

import web, requests

Then setup the URL we want all request to go through

url = (
'/', 'index'
)

Now get your application id, secret and post-login URL you would like to use

app_id = "YOUR_APP_ID"
app_secret = "APP_SECRET"
post_login_url = "http://0.0.0.0:8080/"

This code will have one class index to handle the logic. In this class we want to deal with the authorization code Facebook will return after logging in

user_data = web.input(code=None)
code = user_data.code

From here setup a conditional to check the code

if not code:
    # we are not authorized
    # send to oauth dialog
else:
    # authorized, get access_token

Within the "not authorized" branch, send the user to the dialog

dialog_url = ( "http://www.facebook.com/dialog/oauth?" +
                           "client_id=" + app_id +
                           "&redirect_uri=" + post_login_url +
                           "&scope=publish_stream" )

return "<script>top.location.href='" + dialog_url + "'</script>"

Else we can extract the access_token using the code received

token_url = ( "https://graph.facebook.com/oauth/access_token?" +
                          "client_id=" + app_id +
                          "&redirect_uri=" + post_login_url +
                          "&client_secret=" + app_secret +
                          "&code=" + code )
            response = requests.get(token_url).content

            params = {}
            result = response.split("&", 1)
            for p in result:
                (k,v) = p.split("=")
                params[k] = v

            access_token = params['access_token']

From here you can choose how you want to deal with the call to update the status, for example a form,

graph_url = ( "https://graph.facebook.com/me/feed?" +
"access_token=" + access_token )

return ( '<html><body>' + '\n' +
         '<form enctype="multipart/form-data" action="' +
         graph_url + ' "method="POST">' + '\n' +
         'Say something: ' + '\n' +
         '<input name="message" type="text" value=""><br/><br/>' + '\n' +
         '<input type="submit" value="Send"/><br/>' + '\n' +
         '</form>' + '\n' +
         '</body></html>' )

Or using face.py

from facepy import GraphAPI
graph = GraphAPI(access_token)
try:
    graph.post(
            path = 'me/feed',
            message = 'Your message here'
    )
except GraphAPI.OAuthError, e:
    print e.message

So in the end you can get a slimmed down version like

import web
from facepy import GraphAPI
from urlparse import parse_qs

url = ('/', 'index')

app_id = "YOUR_APP_ID"
app_secret = "APP_SECRET"
post_login_url = "http://0.0.0.0:8080/"

user_data = web.input(code=None)

if not user_data.code:
    dialog_url = ( "http://www.facebook.com/dialog/oauth?" +
                               "client_id=" + app_id +
                               "&redirect_uri=" + post_login_url +
                               "&scope=publish_stream" )

    return "<script>top.location.href='" + dialog_url + "'</script>"
else:
    graph = GraphAPI()
    response = graph.get(
        path='oauth/access_token',
        client_id=app_id,
        client_secret=app_secret,
        redirect_uri=post_login_url,
        code=code
    )
    data = parse_qs(response)
    graph = GraphAPI(data['access_token'][0])
    graph.post(path = 'me/feed', message = 'Your message here')

For more info see

* Facebook API - User Feed: http://developers.facebook.com/docs/reference/api/user/#feed
* Publish a Facebook Photo in Python – The Basic Sauce: http://philippeharewood.com/facebook/publish-a-facebook-photo-in-python-the-basic-sauce/
* Facebook and Python – The Basic Sauce: http://philippeharewood.com/facebook/facebook-and-python-the-basic-sauce/

这篇关于如何使用Python&amp; GraphAPI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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