在python中制作facebook墙贴

Joh*_*nny 4 python facebook

我想在我的脸书粉丝页面上制作一个简单的Wall Post.我有我的APP_ID + APP SECRET,我能够获得访问令牌,但我正在使用facebook.GraphAPI()这是代码:

# -*- coding: utf-8 -*-

import urllib
import facebook

FACEBOOK_APP_ID = '12345'
FACEBOOK_APP_SECRET = '123456789'
FACEBOOK_PROFILE_ID = '321321321321'
oauth_args = dict(
  client_id = FACEBOOK_APP_ID,
  client_secret = FACEBOOK_APP_SECRET,
  grant_type = 'client_credentials')
oauth_response = urllib.urlopen('https://graph.facebook.com/oauth/access_token?'
  + urllib.urlencode(oauth_args)).read()

# oauth_response looks like this:
# access_token=2732467743847839726|3gddzdg3Wl-5S_Go      

attach = {
  "name": 'Hello',
  "link": 'http://www.link.com',
  "caption": 'test',
  "description": 'some test',
  "picture" : 'http://img/picture.png',
}

facebook_graph = facebook.GraphAPI(oauth_response.split('=')[1])
try:
    response = facebook_graph.put_wall_post('', attachment=attach)
except facebook.GraphAPIError as e:
    print e
Run Code Online (Sandbox Code Playgroud)

当我运行脚本时,我收到此错误:

Traceback (most recent call last):
  File "fb.py", line 27, in <module>
    facebook_graph = facebook.GraphAPI(oauth_response.split('=')[1])
AttributeError: 'module' object has no attribute 'GraphAPI'
Run Code Online (Sandbox Code Playgroud)

我在Windows和ubuntu机器上尝试了这个代码,同样的错误.我尝试重新安装facebook模块,但没有任何帮助.任何人都知道如何解决这个问题?

编辑:当我添加import pydoc; pydoc.help(facebook)这是输出:

Help on package facebook:

NAME
    facebook - TODO: Document your package.

FILE
    c:\python26\lib\site-packages\facebook-0.0-py2.6.egg\facebook\__init__.py

PACKAGE CONTENTS


DATA
    __loader__ = <zipimporter object "C:\Python26\lib\site-packages\facebo...
    __version__ = 'TODO: Enter a version'

VERSION
    TODO: Enter a version


Traceback (most recent call last):
  File "fb.py", line 29, in <module>
    facebook_graph = facebook.GraphAPI(oauth_response.split('=')[1])
AttributeError: 'module' object has no attribute 'GraphAPI'
Run Code Online (Sandbox Code Playgroud)

Tho*_*zco 8

PyPI上的Facebook软件包搞砸了.这不是facebook你想要的,但是facebook-sdk.

确保你有一个正确的:

pip uninstall facebook  # Remove the broken package
pip install facebook-sdk  # Install the correct one
Run Code Online (Sandbox Code Playgroud)


Vee*_*rac 6

那个包裹不对.尝试从这个github仓库重新下载和安装.


我刚刚做了同样的事情,这是我的输出pydoc.help(facebook)(也是help在交互式提示下):

Help on module facebook:

NAME
    facebook - Python client library for the Facebook Platform.

FILE
    /usr/lib/python2.7/site-packages/facebook.py

DESCRIPTION
    This client library is designed to support the Graph API and the
    official Facebook JavaScript SDK, which is the canonical way to
    implement Facebook authentication. Read more about the Graph API at
    http://developers.facebook.com/docs/api. You can download the Facebook
    JavaScript SDK at http://github.com/facebook/connect-js/.

    If your application is using Google AppEngine's webapp framework, your
    usage of this module might look like this:

    user = facebook.get_user_from_cookie(self.request.cookies, key, secret)
    if user:
        graph = facebook.GraphAPI(user["access_token"])
        profile = graph.get_object("me")
        friends = graph.get_connections("me", "friends")

CLASSES
    __builtin__.object
        GraphAPI
    exceptions.Exception(exceptions.BaseException)
        GraphAPIError

    class GraphAPI(__builtin__.object)
     |  A client for the Facebook Graph API.
...
Run Code Online (Sandbox Code Playgroud)

所以它没有正确安装.


我建议将facebook.py文件复制到当前目录(与问题中的文件相同)并重新运行.希望你能绕过拙劣的安装.