简体   繁体   English

如何使用Facebook Python SDK从访问令牌中获取用户的电子邮件

[英]How to get a user's email from an access token using the Facebook Python SDK

I am trying to implement Facebook login for an application I am developing. 我正在尝试为我正在开发的应用程序实现Facebook登录。

I am getting an access token like this: 我得到这样的访问令牌:

<!DOCTYPE html>
<html>
<head></head>
<body>
  <script>
window.fbAsyncInit = function() {
    FB.init({
      appId      : '[Redacted]',
      xfbml      : true,
      version    : 'v2.6'
    });

    FB.getLoginStatus(function(response) {
    if (response.status === 'connected') {
        window.location = "http://[my domain]/FBloginAuth?"+FB.getAuthResponse()["accessToken"]
    } else {
        window.location = encodeURI("https://www.facebook.com/dialog/oauth?scope=email&client_id=[redacted]&redirect_uri=http://[my domain]/FBloginAuth&response_type=token");
    }
});

  };

  (function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "http://connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
  </script>

  <div id="status"></div>

</body>
</html>

My server then fishes the access token from the url like this (Python 2, because I have to support the Google OAuth SDK which doesn't seem to support Python 3.): 然后,我的服务器从网址中获取访问令牌,如下所示(Python 2,因为我必须支持似乎不支持Python 3的Google OAuth SDK。):

graph = facebook.GraphAPI(access_token=environ["QUERY_STRING"]) # Apparently there is an extrea
profile = graph.get_object('me')
args = {'fields' : 'id,name,email', }
profile = graph.get_object('me', **args)
print(profile)

I know that this doesn't parse cases where there are variables in the url. 我知道这无法解析网址中存在变量的情况。 That is not the point. 那不是重点。 For now I am hand-feeding the token. 目前,我正在手动输入令牌。 The code vomits: 代码呕吐:

{u'id': u'[redacted]', u'name': u'[redacted]'}

My question is why the email doesn't appear in the response as I set the scope when asking for the token. 我的问题是为什么在我要求令牌时设置范围,为什么电子邮件没有出现在响应中。 Also, what should I do to get the email? 另外,我应该怎么做才能收到电子邮件?

Make sure that 确保

a) the user has actually granted the permission to your app (request /me/permissions to confirm, using the same access token), and a)用户实际上已经授予了您的应用程序的许可(使用相同的访问令牌请求/me/permissions进行确认),以及

b) that their e-mail address is verified with Facebook. b)他们的电子邮件地址已通过Facebook验证。 (Apparently not automatically the case with the e-mail address used to register.) (显然不是自动使用用于注册的电子邮件地址的情况。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Facebook:如何使用python获取/更新用户访问令牌? - Facebook: How to get/update user access token using python? 如何在python中从Facebook的oauth重定向获取访问令牌? - How to get the access token from facebook's oauth redirect in python? Facebook SDK for Python - 获取用户电子邮件 - Facebook SDK for Python - Get user email 在没有访问令牌的情况下从python获取Facebook用户名 - Fetching facebook user's name from python without access token 使用 Python 获取 Facebook 用户访问令牌和使用 Firefox 的本地主机 - Get Facebook user access token using Python and local host with Firefox 如果我不需要用户访问令牌,如何使用请求从Python连接到Facebook Graph API? - How to connect to Facebook Graph API from Python using Requests if I do not need user access token? 如何使用facebook python sdk在facebook中扩展页面的access_token - How to extend the access_token of a page in facebook using facebook python sdk 如何使用Python库获取Facebook访问令牌? - How to get Facebook access token using Python library? 如何使用python获取Facebook OAuth令牌? - How to get facebook oauth token using python? 如何在Python中使用facebook SDK获取facebook帖子的所有评论? - How to get all comments of a facebook post using facebook SDK in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM