无法检索 Facebook 用户属性

发布于 2024-10-16 22:51:28 字数 3364 浏览 1 评论 0原文

我正在关注本教程。我正在尝试检索用户属性; first_name 但结果是 None

不过,我能够检索一些用户属性,例如姓名、图片和电子邮件。

main.py

 class User(db.Model):
        user_id = db.StringProperty(required=True)
        access_token = db.StringProperty(required=True)
        name = db.StringProperty(required=True)
        first_name = db.StringProperty()
        picture = db.StringProperty(required=True)
        email = db.StringProperty()
        friends = db.StringListProperty()
        dirty = db.BooleanProperty()

        def refresh_data(self):
            """Refresh this user's data using the Facebook Graph API"""
            me = Facebook().api(u'/me',
                {u'fields': u'picture,friends', u'access_token': self.access_token})
            self.dirty = False
            self.name = me[u'name']
            self.first_name = me[u'first_name']
            self.email = me.get(u'email')
            self.picture = me[u'picture']
            self.friends = [user[u'id'] for user in me[u'friends'][u'data']]
            return self.put()

...

def init_facebook(self):
    """Sets up the request specific Facebook and User instance"""
    facebook = Facebook()
    user = None

    # initial facebook request comes in as a POST with a signed_request
    if u'signed_request' in self.request.POST:
        facebook.load_signed_request(self.request.get('signed_request'))
        # we reset the method to GET because a request from facebook with a
        # signed_request uses POST for security reasons, despite it
        # actually being a GET. in webapp causes loss of request.POST data.
        self.request.method = u'GET'
        self.set_cookie(
            'u', facebook.user_cookie, datetime.timedelta(minutes=1440))
    elif 'u' in self.request.cookies:
        facebook.load_signed_request(self.request.cookies.get('u'))

    # try to load or create a user object
    if facebook.user_id:
        user = User.get_by_key_name(facebook.user_id)
        if user:
            # update stored access_token
            if facebook.access_token and \
                    facebook.access_token != user.access_token:
                user.access_token = facebook.access_token
                user.put()
            # refresh data if we failed in doing so after a realtime ping
            if user.dirty:
                user.refresh_data()
            # restore stored access_token if necessary
            if not facebook.access_token:
                facebook.access_token = user.access_token

        if not user and facebook.access_token:
            me = facebook.api(u'/me', {u'fields': u'picture,friends'})
            user = User(key_name=facebook.user_id,
                user_id=facebook.user_id,
                access_token=facebook.access_token,
                name=me[u'name'],
                first_name=facebook.first_name,
                email=me.get(u'email'),  # optional
                picture=me[u'picture'],
                friends=[user[u'id'] for user in me[u'friends'][u'data']])
            user.put()

    self.facebook = facebook
    self.user = user

runs.html

<strong>first name:</strong>  
    {{ logged_in_user.first_name }}

I am following this tutorial. I am trying to retrieve the user properties; first_name but I got None as the result.

I am able to retrieve some user properties like name, picture and email though.

main.py

 class User(db.Model):
        user_id = db.StringProperty(required=True)
        access_token = db.StringProperty(required=True)
        name = db.StringProperty(required=True)
        first_name = db.StringProperty()
        picture = db.StringProperty(required=True)
        email = db.StringProperty()
        friends = db.StringListProperty()
        dirty = db.BooleanProperty()

        def refresh_data(self):
            """Refresh this user's data using the Facebook Graph API"""
            me = Facebook().api(u'/me',
                {u'fields': u'picture,friends', u'access_token': self.access_token})
            self.dirty = False
            self.name = me[u'name']
            self.first_name = me[u'first_name']
            self.email = me.get(u'email')
            self.picture = me[u'picture']
            self.friends = [user[u'id'] for user in me[u'friends'][u'data']]
            return self.put()

...

def init_facebook(self):
    """Sets up the request specific Facebook and User instance"""
    facebook = Facebook()
    user = None

    # initial facebook request comes in as a POST with a signed_request
    if u'signed_request' in self.request.POST:
        facebook.load_signed_request(self.request.get('signed_request'))
        # we reset the method to GET because a request from facebook with a
        # signed_request uses POST for security reasons, despite it
        # actually being a GET. in webapp causes loss of request.POST data.
        self.request.method = u'GET'
        self.set_cookie(
            'u', facebook.user_cookie, datetime.timedelta(minutes=1440))
    elif 'u' in self.request.cookies:
        facebook.load_signed_request(self.request.cookies.get('u'))

    # try to load or create a user object
    if facebook.user_id:
        user = User.get_by_key_name(facebook.user_id)
        if user:
            # update stored access_token
            if facebook.access_token and \
                    facebook.access_token != user.access_token:
                user.access_token = facebook.access_token
                user.put()
            # refresh data if we failed in doing so after a realtime ping
            if user.dirty:
                user.refresh_data()
            # restore stored access_token if necessary
            if not facebook.access_token:
                facebook.access_token = user.access_token

        if not user and facebook.access_token:
            me = facebook.api(u'/me', {u'fields': u'picture,friends'})
            user = User(key_name=facebook.user_id,
                user_id=facebook.user_id,
                access_token=facebook.access_token,
                name=me[u'name'],
                first_name=facebook.first_name,
                email=me.get(u'email'),  # optional
                picture=me[u'picture'],
                friends=[user[u'id'] for user in me[u'friends'][u'data']])
            user.put()

    self.facebook = facebook
    self.user = user

runs.html

<strong>first name:</strong>  
    {{ logged_in_user.first_name }}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小清晰的声音 2024-10-23 22:51:28

我认为这是因为您仅通过“图片”询问,而“朋友”

me = Facebook().api(u'/me',
                {u'fields': u'picture,friends', u'access_token': self.access_token})

仅尝试使用 access_token

me = Facebook().api(u'/me',
                {u'access_token': self.access_token})

I think it is becouse you are asking only by "picture" and "friends"

me = Facebook().api(u'/me',
                {u'fields': u'picture,friends', u'access_token': self.access_token})

try only with access_token

me = Facebook().api(u'/me',
                {u'access_token': self.access_token})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文