Facebook Graph API 'module' object has no attribute 'GraphAPI' - facebook-graph-api

I have the following simple example in Python 2.7 (using the Anaconda distribution) for accessing data using the Facebook Graph API:
import facebook
token = 'mytoken'
graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")
friend_list = [friend['name'] for friend in friends['data']]
print friend_list
Initially I got the 'module' object has no attribute 'GraphAPI' and noticed that the prepopulated list of methods for facebook was basically empty. This led me to take the advice of this post and uninstall facebook and install facebook-sdk. This worked in the sense that now GraphAPI shows up in the prepopulated list, but I still get the following error:
AttributeError: 'module' object has no attribute 'GraphAPI'
Is there another solution to this problem I might be overlooking?

This is a known Problem. Just uninstall facebook and facebook-sdk and then reinstall only facebook-sdk.
GraphApi module present in pyfacebook or not
Python Facebook SDK: 'module' object has no attribute 'GraphAPI'
Update1:
I think I got it now. I think you aren't using the token. Just try this:
import facebook
token = 'mytoken'
graph = facebook.GraphAPI(token)
profile = graph.get_object("me")
friends = graph.get_connections("me", "friends")
friend_list = [friend['name'] for friend in friends['data']]
print friend_list

May be obvious, but make sure your python script isn't also named facebook.py, as it will clash with the package and cause this error.

Related

Rails 4: ActionMailer Preview cannot find Class

I am trying to preview an email and I'm running into some trouble.
# test/mailers/previews/food_order_preview.rb
class FoodOrderPreview < ActionMailer::Preview
def food_order_email
#food_order = FoodOrder.first # line with error
#fields = #food_order.with_values
FoodOrder.food_order_email(order, fields)
end
end
When I load the preview I see:
NoMethodError (undefined method `first' for FoodOrder:Class):
test/mailers/previews/food_order_preview.rb:6:in `food_order_email'
Why would this occur?
Thanks to this SO Question I was able to diagnose the issue. There was an issue with the migration I ran while creating the mailer.
THE ROOT CAUSE OF THE PROBLEM
When I created the Invite mailer, I ran rails g mailer Invite instead
of rails g mailer InviteMailer.
Because of this, Invite as a mailer override Invite as a model, hence
creating errors as soon as methods were applied to instances of the
Invite model.
HOW WE FIXED IT
Once we had identified the issue, fixing it was pretty
straightforward:
We changed the name of the Invite mailer from invite.rb to
invite_mailer.rb
In the newly renamed invite_mailer.rb file, we
replaced class Invite < ApplicationMailer with class InviteMailer <
ApplicationMailer
Hope this helps someone else!

Biopython Entrez wanting to access /root/.config in Django app

I have some code in a Django app which does the following, to get a Pubmed article by DOI:
def getPubmedByDOI(request,doi):
Entrez.email = 'me#mydomain.com'
handle = Entrez.esearch(db="pubmed", term=doi)
record = Entrez.read(handle)
return getPubmedArticle(request,record["IdList"][0]) // renders the article
This works nicely but for one thing - the Entrez.esearch call insists upon access to /root/.config on the server, specifically to write to the following empty directory:
/root/.config/biopython/Bio/Entrez/DTDs/
It's Apache on Gentoo, running as follows:
User django
Group apache
All the code for the application is in ~django/, so I'd expect any writing to be in ~django/.config rather than /root/.config. I can work around this by changing permissions on /root but a better solution would be to configure Biopython or Apache so as not to write to /root. Does anyone have any suggestions as to how this might be done?
Logged upstream as https://github.com/biopython/biopython/issues/918 which suggests setting:
>>> from Bio.Entrez.Parser import DataHandler
>>> DataHandler.global_dtd_dir
'/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/Entrez/DTDs'
>>> DataHandler.local_dtd_dir = '...'

GQL Queries - Retrieving specific data from query object

I'm building a database using Google Datastore. Here is my model...
class UserInfo(db.Model):
name = db.StringProperty(required = True)
password = db.StringProperty(required = True)
email = db.StringProperty(required = False)
...and below is my GQL Query. How would I go about retrieving the user's password and ID from the user_data object? I've gone through all the google documentation, find it hard to follow, and have spent ages trying various things I've read online but nothing has helped! I'm on Python 2.7.
user_data = db.GqlQuery('SELECT * FROM UserInfo WHERE name=:1', name_in)
user_info = user_data.get()
This is basic Python.
From the query, you get a UserInfo instance, which you have stored in the user_info variable. You can access the data of an instance via dot notation: user_info.password and user_info.email.
If this isn't clear, you really should do a basic Python tutorial before going any further.
You are almost there. Treat the query object like a class.
name = user_info.name
Documentation on queries here gives some examples
There are some python tips that might help you
dir(user_info)
help(user_info)
you can also print almost anything, like
print user_info[0]
print user_info[0].name
Setup logging for your app
Logging and python etc

How should I get Open Graph JSON object to pass in facepy class

I am trying to configure Open Graph in my app such that, when ever a person click a "link" in it, the app should "post" it on his feeds/ timeline/ activity. (I created open graph actions for these features and successfully added meta tags in the page).
So, I am using fandjango and facepy to do this job.
This is how my code looks..
from facepy import GraphAPI
#csrf_exempt
#facebook_authorization_required(permissions=["publish_actions"])
def ViewPage (request):
access_token = request.facebook.user.oauth_token
profile_id = request.facebook.user.facebook_id
path = "/%d/feed" %profile_id
# How should I get the OpenGraph JSON obj
og_data = ??
graph = GraphAPI(access_token)
graph.post(path, og_data)
...
return render_to_response("view.html", context)
How should I get the open graph JSON obj to pass in the above graph object as parameter so as to post data in feeds/ timeline/ activity?
If this is not the way, how should I do it?
Edit 1:
when I tried
graph = GraphAPI(request.facebook.user.oauth_token)
graph.post("me/namespace:action")
It showed me `OAuthError`
Error Loc: C:\Python27\lib\site-packages\facepy\graph_api.py in _parse, line 274
graph = GraphAPI(request.facebook.user.oauth_token)
graph.post("me/namespace:action", "object type")
It showed me `TypeError`
loc: same as previous
Edit 2:
Instead to using request.facebook.user.oauth_token, I directly used my access token and the code worked..
graph = GraphAPI (my-hard-coded-access-token)
graph.post("me/feed", message = "working!")
However, when I tried
graph.post("me/news.reads", article="working")
It showed me error.
I created open graph actions
You shouldn't be using me/feed then
The OpenGraph calls are as follows
me/[app_namespace]:[action_type]?[object_type]=[OBJECT_URL]
So, to achieve the same just set the path to me/[app_namespace]:[action_type]
graph.post(
path = 'me/[app_namespace]:[action_type]',
[object_type] = '[OBJECT_URL]'
)
og_data is not a parameter in the call. So if your object_type is recipe then it would be
graph.post(path, recipe)
If you want to continue to use me/feed then it should be
graph.post(path, link)
as described at http://developers.facebook.com/docs/reference/api/user/#posts
You cannot do customs read actions like that, the proper call should be
graph.post(
path = 'me/news.reads',
article = 'http://yourobjecturl/article.html'
)
Please read the documentation http://developers.facebook.com/docs/opengraph/actions/builtin/#read
You're getting an OAuthError because you're initializing the Graph API client with an instance of a class instead of a string.
Try this:
graph = GraphAPI(request.facebook.user.oauth_token.token)
Alternatively, you could use the graph propery of the Fandjango's User model to get a preinitialized instance of the Graph API client:
request.facebook.user.graph

Django error: 'QueryDict' object has no attribute '_meta'

I am very new to django so this is probably a noob question.
I am trying to reuse django admin's change list view. I've created a admin model and want to provide the changelist template a list of these objects. In my view I have:
def placements(request):
partner_id = request.session.get('partner_id', 0)
self = PlacementAdmin(request.GET, Placement.objects.filter(partner=partner_id))
return render_to_response('publisher/placement/change_list.html', {'cl': self})
I get this error when I try to hit this function from a browser:
'QueryDict' object has no attribute '_meta'
Could anyone tell me what the error is or provide an easier way to accomplish this in case I am on the wrong track completely.
Heres the complete trace:
Request Method: GET
Request URL: http://localhost:8080/publisher/
Django Version: 1.3 beta 1
Exception Type: AttributeError
Exception Value:
'QueryDict' object has no attribute '_meta'
Exception Location: /Users/imran/django_env/lib/python2.6/site-packages/django/contrib/admin/options.py in __init__, line 278
Python Executable: /Users/imran/django_env/bin/python
Python Version: 2.6.1
Python Path:
['.',
'.',
'/Users/imran/Workspaces/publisher/django/pub_admin',
'/Users/imran/django_env/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg',
'/Users/imran/django_env/lib/python2.6/site-packages/pip-0.8.1-py2.6.egg',
'/Users/imran/django_env/lib/python26.zip',
'/Users/imran/django_env/lib/python2.6',
'/Users/imran/django_env/lib/python2.6/plat-darwin',
'/Users/imran/django_env/lib/python2.6/plat-mac',
'/Users/imran/django_env/lib/python2.6/plat-mac/lib-scriptpackages',
'/Users/imran/django_env/Extras/lib/python',
'/Users/imran/django_env/lib/python2.6/lib-tk',
'/Users/imran/django_env/lib/python2.6/lib-old',
'/Users/imran/django_env/lib/python2.6/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages',
'/Users/imran/django_env/lib/python2.6/site-packages']
It's best to post the traceback error when posting a question!
That said, the error comes from you passing request.GET to a ModelAdmin admin object which isn't expecting it.
You have a long road of troubles ahead of you though, as you're trying to use the magical change_list view template which is operated on by a collection of even more magical, undocumented template tags, which just doesn't normally appear in the same sentence as new to django.
I think you're on the wrong track simply because django's admin is not easy to hack into.
At a minimum, you'd have to pass in your template a ChangeList object for the cl variable.
If you really want to do this, the only advice I can give is to take a look at django.contrib.admin.options.ModelAdmin.changelist_view() since that's what you're trying to replicate.
Seriously though I'd like to talk you out of this. Django's actually really fun to work with!