Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在GAE Python中使用Facebook Graph API获取用户电子邮件?_Python_Google App Engine_Facebook_Authentication_Facebook Graph Api - Fatal编程技术网

如何在GAE Python中使用Facebook Graph API获取用户电子邮件?

如何在GAE Python中使用Facebook Graph API获取用户电子邮件?,python,google-app-engine,facebook,authentication,facebook-graph-api,Python,Google App Engine,Facebook,Authentication,Facebook Graph Api,我在谷歌应用程序引擎上使用Facebook图形API。我能够从用户那里获取所有基本信息。然而,当我试图获取任何需要权限的用户信息时,例如电子邮件,它总是显示为无。我已经阅读了网站上提供的全部教程 这是我的密码: class User(db.Model): id = db.StringProperty(required=True) created = db.DateTimeProperty(auto_now_add=True) updated = db.DateTimePr

我在谷歌应用程序引擎上使用Facebook图形API。我能够从用户那里获取所有基本信息。然而,当我试图获取任何需要权限的用户信息时,例如电子邮件,它总是显示为无。我已经阅读了网站上提供的全部教程

这是我的密码:

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
以下是模板/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 %}

{%if当前用户%}

你好,{{current_user.name | escape}}

电子邮件:{current_user.email}

{%endif%}

有什么不对劲吗?是否有其他方法获取用户电子邮件?

您的代码看起来正确,因此我假设您没有正确的扩展权限。如果您要尝试获取电子邮件地址,必须申请“电子邮件”扩展权限。您可以找到扩展权限列表。在使用Graph API读取这些属性之前,必须请求这些权限。

电子邮件是在用户创建时编写的。可能您正在尝试访问在您没有电子邮件权限时创建的用户,因此书面电子邮件为“无”。新用户对象也会出现问题吗?

刚刚放弃使用并使用urlfetch创建了自己的OAuth 2客户端。请参阅Facebook文档


另外,我将
scope='email'
放在
https://graph.facebook.com/oauth/authorize?
https://graph.facebook.com/oauth/access_token?

他在登录按钮标签中有perms=“email”。这将提示您输入权限。请尝试添加从FB获得的配置文件的调试输出,并查看电子邮件是否存在。当FB授权对话框出现时,是否包含电子邮件权限?