Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get user email using Facebook Graph API in GAE Python?

I'm using Facebook Graph API on Google App Engine. I was able to fetch all the basic info from an user. However when I tried to fetch any user info that requires permission, email for exemple, it always appears as None. I've followed the whole tutorial available at the developers blog.

Here's my code:

class User(db.Model):
    id = db.StringProperty(required=True)
    created = db.DateTimeProperty(auto_now_add=True)
    updated = db.DateTimeProperty(auto_now=True)
    name = db.StringProperty(required=True)
    email = db.StringProperty(required=True)
    profile_url = db.StringProperty(required=True)
    access_token = db.StringProperty(required=True)


class BaseHandler(webapp.RequestHandler):
    """Provides access to the active Facebook user in self.current_user

    The property is lazy-loaded on first access, using the cookie saved
    by the Facebook JavaScript SDK to determine the user ID of the active
    user. See http://developers.facebook.com/docs/authentication/ for
    more information.
    """
    @property
    def current_user(self):
        if not hasattr(self, "_current_user"):
            self._current_user = None
            cookie = facebook.get_user_from_cookie(
                self.request.cookies, FACEBOOK_APP_ID, FACEBOOK_APP_SECRET)
            if cookie:
                # Store a local instance of the user data so we don't need
                # a round-trip to Facebook on every request
                user = User.get_by_key_name(cookie["uid"])
                if not user:
                    graph = facebook.GraphAPI(cookie["access_token"])
                    profile = graph.get_object("me")
                    user = User(key_name=str(profile["id"]),
                                id=str(profile["id"]),
                                name=profile["name"],
                                email=profile["email"],
                                profile_url=profile["link"],
                                access_token=cookie["access_token"])
                    user.put()
                elif user.access_token != cookie["access_token"]:
                    user.access_token = cookie["access_token"]
                    user.put()
                self._current_user = user
        return self._current_user

And here is the template/HTML:

<fb:login-button autologoutlink="true" perms="email"></fb:login-button>

{% if current_user %}
  <p><a href="{{ current_user.profile_url }}"><img src="http://graph.facebook.com/{{ current_user.id }}/picture?type=square"/></a></p>
  <p>Hello, {{ current_user.name|escape }}</p>
  <p>email: {{ current_user.email }} </p>
{% endif %}

Is there something wrong? Is there other way to get user email?

like image 715
fjsj Avatar asked Oct 23 '10 20:10

fjsj


People also ask

How do I get data from Facebook Graph API?

Open the Graph Explorer in a new browser window. This allows you to execute the examples as you read this tutorial. The explorer loads with a default query with the GET method, the lastest version of the Graph API, the /me node and the id and name fields in the Query String Field, and your Facebook App.

How do I find my Facebook API user ID?

Create new app by clicking Add New App. Enter all the required details like name, email id and click on Create APP ID to get APP ID and APP SECRET to access the Facebook API.


2 Answers

Email is written on User creation. Maybe you are trying to access users that were created when you didn't have email permission, therefore the written email was None. Does the problem appear also for new User object?

like image 133
user375348 Avatar answered Oct 20 '22 13:10

user375348


Just gave up using facebook.py and facebookoauth.py and made my own OAuth 2 client using urlfetch. See 'Authenticating Users in a Web Application' in Facebook docs.

Also, I put scope='email' in requests to https://graph.facebook.com/oauth/authorize? and https://graph.facebook.com/oauth/access_token?.

like image 1
fjsj Avatar answered Oct 20 '22 13:10

fjsj