添加include_email参数时无法在Twitter上验证错误

时间:2017-09-25 21:18:01

标签: python twitter flask twitter-oauth

我正在使用Miguel Grinberg's example允许用户通过Twitter和Flask-Login登录。它工作正常,但当我请求用户的电子邮件(?include_email = true)时,我收到以下错误:

{u'errors': [{u'message': u'Could not authenticate you.', u'code': 32}]}

在apps.twitter.com上,我启用了从用户那里请求电子邮件地址。我可以在同意屏幕上看到申请电子邮件的应用。我也试过重新生成我的消费者密钥和消费者秘密,但没有运气。

以下是使用Twitter进行身份验证的代码:

class TwitterSignIn(OAuthSignIn):
    def __init__(self):
        super(TwitterSignIn, self).__init__('twitter')
        self.service = OAuth1Service(
            name='twitter',
            consumer_key=self.consumer_id,
            consumer_secret=self.consumer_secret,
            request_token_url='https://api.twitter.com/oauth/request_token',
            authorize_url='https://api.twitter.com/oauth/authenticate',
            access_token_url='https://api.twitter.com/oauth/access_token',
            base_url='https://api.twitter.com/1.1/'
        )

    def authorize(self):
        request_token = self.service.get_request_token(
            params={'oauth_callback': self.get_callback_url()}
        )
        session['request_token'] = request_token
        return redirect(self.service.get_authorize_url(request_token[0]))

    def callback(self):
        request_token = session.pop('request_token')
        if 'oauth_verifier' not in request.args:
            return None, None, None
        oauth_session = self.service.get_auth_session(
            request_token[0],
            request_token[1],
            data={'oauth_verifier': request.args['oauth_verifier']}
        )
        network = 'twitter'
        me = oauth_session.get('account/verify_credentials.json?include_email=true').json()
        social_id = 'twitter$' + str(me.get('id'))
        username = me.get('screen_name')
        email = me.get('email')
        return social_id, username, email

0 个答案:

没有答案