我正在研究有关社会认证的Miguel Grinberg教程。

在主页模板上,我有以下代码,并且从教程中删除了twitter部分:

    <h2>I don't know you!</h2>
    <p><a href="{{ url_for('oauth_authorize', provider='facebook') }}">Login with Facebook</a></p>
{% endif %}


因此,当您单击该链接时,通过此视图功能将Facebook作为提供者:

@app.route('/authorize/<provider>')
def oauth_authorize(provider):
    if not current_user.is_anonymous():
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    return oauth.authorize()


现在,在另一个文件oauth.py中,我有以下内容,而我的问题是这个。如果不删除TwitterSignIn类,则单击Facebook链接时,我总是收到错误消息。我想知道为什么需要删除TwitterSignIn类才能使它起作用,因为没有数据传递给它,对吗?即使不是Facebook的唯一选择,为什么还要单击Facebook登录链接将任何数据传递给TwitterSignIn类?

from rauth import OAuth1Service, OAuth2Service
from flask import current_app, url_for, request, redirect, session


class OAuthSignIn(object):
    providers = None

    def __init__(self, provider_name):
        self.provider_name = provider_name
        credentials = current_app.config['OAUTH_CREDENTIALS'][provider_name]
        self.consumer_id = credentials['id']
        self.consumer_secret = credentials['secret']

    def authorize(self):
        pass

    def callback(self):
        pass

    def get_callback_url(self):
        return url_for('oauth_callback', provider=self.provider_name,
                       _external=True)

    @classmethod
    def get_provider(self, provider_name):
        if self.providers is None:
            self.providers = {}
            for provider_class in self.__subclasses__():
                provider = provider_class()
                self.providers[provider.provider_name] = provider
        return self.providers[provider_name]


class FacebookSignIn(OAuthSignIn):
    def __init__(self):
        super(FacebookSignIn, self).__init__('facebook')
        self.service = OAuth2Service(
            name='facebook',
            client_id=self.consumer_id,
            client_secret=self.consumer_secret,
            authorize_url='https://graph.facebook.com/oauth/authorize',
            access_token_url='https://graph.facebook.com/oauth/access_token',
            base_url='https://graph.facebook.com/'
        )

    def authorize(self):
        return redirect(self.service.get_authorize_url(
            scope='email',
            response_type='code',
            redirect_uri=self.get_callback_url())
        )

    def callback(self):
        if 'code' not in request.args:
            return None, None, None
        oauth_session = self.service.get_auth_session(
            data={'code': request.args['code'],
                  'grant_type': 'authorization_code',
                  'redirect_uri': self.get_callback_url()}
        )
        me = oauth_session.get('me').json()
        return (
            'facebook$' + me['id'],
            me.get('email').split('@')[0],  # Facebook does not provide
                                            # username, so the email's user
                                            # is used instead
            me.get('email')
        )


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/authorize',
            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']}
        )
        me = oauth_session.get('account/verify_credentials.json').json()
        social_id = 'twitter$' + str(me.get('id'))
        username = me.get('screen_name')
        return social_id, username, None   # Twitter does not provide email


一些其他信息-

具体错误是这样的:

File "/Users/metersky/code/mylastapt/app/oauth.py", line 29, in get_provider
provider = provider_class()
File "/Users/metersky/code/mylastapt/app/oauth.py", line 73, in __init__
super(TwitterSignIn, self).__init__('twitter')
File "/Users/metersky/code/mylastapt/app/oauth.py", line 10, in __init__
credentials = current_app.config['OAUTH_CREDENTIALS'][provider_name]
KeyError: 'twitter'


我认为这可能是问题所在:

app.config['OAUTH_CREDENTIALS'] = {
    'facebook': {
        'id': 'XXX',
        'secret': 'XXXX'
    }
}

最佳答案

问题出在OAuthSignIn.get_provider中。

@classmethod
def get_provider(self, provider_name):
    if self.providers is None:
        self.providers = {}
        for provider_class in self.__subclasses__():
            provider = provider_class()
            self.providers[provider.provider_name] = provider
    return self.providers[provider_name]


第一次从您的视图中调用它

oauth = OAuthSignIn.get_provider(provider)


该方法将缓存您已定义的提供程序。它通过检查所有OAuthSignIn的子类来实现。

for provider_class in self.__subclasses__():


当包含TwitterSignIn时,它将作为子类包含在内。然后,您将实例化该类的实例

provider = provider_class()


OAuthSignIn.__init__中,您可以使用current_app.config['OAUTH_CREDENTIALS'][provider_name]加载提供商的设置。由于不包括Twitter,因此您会获得KeyError

如果您不想支持Twitter,只需删除该类。如果您想进一步保护应用程序,以便可以在不更新代码的情况下将提供程序从设置中删除,则需要检查该异常。您可以在OAuthSignIn.__init__中进行检查,但是在OAuthSignIn.providers中包含不受支持的提供程序可能没有太大价值。您最好将支票放入OAuthSignIn.get_provider

@classmethod
def get_provider(cls, provider_name):
    if cls.providers is None:
        cls.providers = {}
        for provider_class in cls.__subclassess__():
            try:
                provider = provider_class()
            except KeyError:
                pass  # unsupported provider
            else:
                cls.providers[provider.provider_name] = provider
    return cls.providers[provider_name]

10-06 00:28