Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make facebook wall post in python

I want to make a simple Wall Post on my facebook fanpage. I have my APP_ID + APP SECRET and I'm able to get the access token but I'm struggeling with facebook.GraphAPI() This is the code:

# -*- 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

When I run the script, I get this error:

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'

I tried this code on a windows and ubuntu machine, same error. I tried reinstalling the facebook module but nothing helped. Anyone has a clue how to solve this issue?

EDIT: when I add import pydoc; pydoc.help(facebook) this is the output:

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'
like image 759
Johnny Avatar asked Dec 08 '22 12:12

Johnny


2 Answers

The Facebook package on PyPI is messed up. It's not facebook that you want, but facebook-sdk.

Make sure you have the right one:

pip uninstall facebook  # Remove the broken package
pip install facebook-sdk  # Install the correct one
like image 179
Thomas Orozco Avatar answered Dec 15 '22 00:12

Thomas Orozco


That package isn't right. Try re-downloading and installing from this github repo.


I just did the same and this is my output from pydoc.help(facebook) (which is also help at the interactive prompt):

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.
...

so it hasn't installed properly.


I suggest copying the facebook.py file to your current directory (the same as the file from the question) and re-running. Hopefully you can circumvent the botched install.

like image 24
Veedrac Avatar answered Dec 14 '22 23:12

Veedrac