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)
crea开发者_JAVA技巧ted = 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?
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?
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?
.
Your code looks correct so I would assume that you don't have the correct extended permissions. You must ask for the "email" extended permission if you are going to try to get the email address. You can find the list of extended permissions here. You must ask for these permissions before you can read these properties using the Graph API.
精彩评论