Google and Twitter return empty user data on Flask

我们两清 提交于 2019-12-12 02:28:43

问题


I don't understand what is wrong? callback uri:

google - http://127.0.0.1:5000/auth/google/
twitter - http://127.0.0.1:5000/auth/tw/

config:

from authomatic.providers import oauth2, oauth1
SECRET_KEY = '####'
AUTH_CONFIG = {
    'google': {
        'class_': oauth2.Google,
        'consumer_key': '####',
        'consumer_secret': ####',
        'scope': ['email',],
    },
    'tw': {
        'class_': oauth1.Twitter,
        'consumer_key': '####',
        'consumer_secret': '####',
    },
}

controller:

from authomatic.adapters import WerkzeugAdapter
from authomatic import Authomatic
from app import app, db
from app.models.users import User

authomatic = Authomatic(
    app.config.get('AUTH_CONFIG'),
    app.config.get('SECRET_KEY'),
    report_errors=True
)
@app.route('/auth/<provider>/', methods=['GET', 'POST'])
def auth(provider):
    print "REQUEST: ", request.args
    response = make_response()
    result = authomatic.login(WerkzeugAdapter(request, response), provider)
    if result:
        if result.user:
            result.user.update()
            if result.user.email:
                user = User.query.filter(User.email == result.user.email).first()
                if user is None:
                    user = User(nickname=result.user.name, email=result.user.email)
                    db.session.add(user)
                    db.session.commit()
                flash('A new user profile has been created for you.')
                return redirect(url_for('index'))
            flash('Your provider return empty data, try again later.')
            return redirect(url_for('index'))
    return response

and after I accept access to app in google or twitter, I have redirected to index.html page with flash massage "Your provider return empty data, try again later"

in console I see:

google:
127.0.0.1 - - [29/Nov/2014 00:41:26] "GET /login/ HTTP/1.1" 200 -
REQUEST:  ImmutableMultiDict([])
127.0.0.1 - - [29/Nov/2014 00:41:27] "GET /auth/google/ HTTP/1.1" 302 -
REQUEST:  ImmutableMultiDict([('state', u'bbee8547ff97e001sdss61e6'), ('code', u'4/ZJRhjCqEzAVep9UL2epaTzYI')])
127.0.0.1 - - [29/Nov/2014 00:41:30] "GET /auth/google/?state=bbee8547ff97e001d3d77161e6&code=4/ZJRhjCqEzAVep9UL2epaTzYI HTTP/1.1" 302 -
127.0.0.1 - - [29/Nov/2014 00:41:30] "GET / HTTP/1.1" 200 -

twitter:
127.0.0.1 - - [29/Nov/2014 00:43:38] "GET /login/ HTTP/1.1" 200 -
REQUEST:  ImmutableMultiDict([])
127.0.0.1 - - [29/Nov/2014 00:43:42] "GET /auth/tw/ HTTP/1.1" 302 -
REQUEST:  ImmutableMultiDict([('oauth_token', u'KmF9L1m5CYUY9O6joIh0'), ('oauth_verifier', u'95sGsiRz5sTxZua88G')])
127.0.0.1 - - [29/Nov/2014 00:43:44] "GET /auth/tw/?oauth_token=KmF9L1m5CYUY9O6joIh0&oauth_verifier=95sGsiRz5sTxZua88G HTTP/1.1" 302 -
127.0.0.1 - - [29/Nov/2014 00:43:44] "GET / HTTP/1.1" 200 -

May be some thing wrong if i get 302 - on response???

Please help me!


回答1:


I believe this is a setup issue on the Google Developer Console.

Under the APIs & auth settings of your project you need to authorize the API's you want access to, otherwise nothing will be returned in the results var.

If you inspect it you will see an error message from Google telling you to authorize API's.

Add the following API's from here: APIs & auth > APIs, select Google+ API, this will return the result dictionary with the values you are looking for.

from authomatic.providers import openid, oauth2

CONFIG = {
    'oi': {

        # OpenID provider dependent on the python-openid package.
        'class_': openid.OpenID,
    },
    'google' : {
        'class_': oauth2.Google,
        'consumer_key': 'GOOGLE DEVELOPER CLIENT ID',
        'consumer_secret': 'GOOGLE DEVELOPER CLIENT SECRET',
        'scope': ['profile', 'email']
    }
}

on your index.html you will want to trigger the call like:

<a href="login/google" method="GET">Sign In With Google</a>

That should trigger the call to the google oauth login.

@app.route('/login/<provider_name>/', methods=['GET', 'POST'])
def login(provider_name):
    """
    Login handler, must accept both GET and POST to be able to use OpenID.
    """

    # We need response object for the WerkzeugAdapter.
    response = make_response()

    # Log the user in, pass it the adapter and the provider name.
    result = authomatic.login(WerkzeugAdapter(request, response), provider_name)

    # If there is no LoginResult object, the login procedure is still pending.
    if result:
        if result.user:
            # We need to update the user to get more info.
            result.user.update()

        # The rest happens inside the template.
        #result.user.data.x will return further user data
        return render_template('login.html', email=result.user.email, name=result.user.name)

    # Don't forget to return the response.
    return response


来源:https://stackoverflow.com/questions/27196887/google-and-twitter-return-empty-user-data-on-flask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!