Tornado facebook_request()收到电子邮件

时间:2016-09-30 01:14:11

标签: facebook facebook-graph-api tornado

我正在使用龙卷风并试图从图谱API获取Facebook用户的电子邮件地址。我有以下代码(大部分来自Tornado网站)

class FacebookAuth2Handler(BaseHandler,tornado.auth.FacebookGraphMixin):
  @tornado.gen.coroutine
  def get(self):
      if self.get_argument("code", False):
          user = yield self.get_authenticated_user(redirect_uri=self.settings["facebook_redirect_uri"],
                                                   client_id=self.settings["facebook_app_id"],
                                                   client_secret=self.settings["facebook_secret"],
                                                   code=self.get_argument("code"))
          ob = yield self.facebook_request("/me/email",access_token=user["access_token"])
          print(ob)

      else:
          yield self.authorize_redirect(redirect_uri=self.settings["facebook_redirect_uri"],
                                        client_id=self.settings["facebook_app_id"],
                                        extra_params={"scope": ["email","public_profile"]})

问题似乎是使用facebook_request()获取/ me / email,这会导致以下内容崩溃:

tornado.auth.AuthError: Error response HTTP 400: Bad Request fetching https://graph.facebook.com/me/email?access_token=xxxxxxx

1 个答案:

答案 0 :(得分:1)

将路径设置为"/me/email"无效,并将其设置为"/me?fields=email"会导致其将您的网址发送为"/me?fields=email?access_token=xxxxxxx",这也不好。

使用fields参数:

ob = yield self.facebook_request(
    path="/me",
    access_token=user["access_token"],
    fields="email,gender"
)

或者您可以通过将extra_fields参数添加到get_authenticated_user来真正简化操作。请注意,它是一个python list,而不是像上面那样的逗号分隔字符串:

user = yield self.get_authenticated_user(redirect_uri=self.settings["facebook_redirect_uri"],
                                         client_id=self.settings["facebook_app_id"],
                                         client_secret=self.settings["facebook_secret"],
                                         code=self.get_argument("code"),
                                         extra_fields=['email','gender']
)

任何丢失或未允许的字段都会在返回的None映射对象中显示为user