Google和Twitter在Flask上返回空的用户数据 [英] Google and Twitter return empty user data on Flask

查看:156
本文介绍了Google和Twitter在Flask上返回空的用户数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白什么是错的?
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':{$ b $'class_':oauth1.Twitter,
'consumer_key':' ####',
'consumer_secret':'####',
},
}


$ b $

控制器:



  from authomatic.adapters import WerkzeugAdapter 
from authomatic import Authomatic
从应用程序导入应用程序,db
从app.models.users导入用户

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):
printREQUEST:,request.args
response = make_response()
result = authomatic。 login(WerkzeugAdapter(request,response),provider)
如果结果:
如果result.user:
result.user.update()$ b $如果result.user.email:
user = User.query.filter(User.email == result.user.email).first()
如果用户是None:
user = User(nickname = result.user.name ,email = result.user.email)
db.session.add(user)
db.session.commit()
flash('已经创建了一个新的用户配置文件。 )
返回重定向(url_for('index'))
flash('你的提供者返回空数据,稍后再试。')
返回重定向(url_for('index'))
返回响应

在应用程序在谷歌或叽叽喳喳,我已经重定向到index.html网页与闪光按摩您的提供商返回空数据,稍后再试



在控制台我看到:

  google:
127.0.0.1 - - [29 / Nov / 2014 00:41:26]GET / login / HTTP / 1.1200 -
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.1302 -
127.0.0.1 - - [29 / Nov / 2014 00:41:30 ]GET / HTTP / 1.1200 -

twitter:
127.0.0.1 - - [29 / Nov / 2014 00:43:38]GET / login / HTTP / 1.1 200 -
请求:ImmutableMultiDict([])
127.0.0.1 - [29 / Nov / 2014 00:43:42]GET / auth / tw / HTTP / 1.1302 -
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.1302 -
127.0.0.1 - - [29 / Nov / 2014 00:43:44]GET / HTTP / 1.1200 -

如果我得到302,可能会出现一些问题 - 请回复???



请帮助我! / b>

解决方案

我相信这是Google开发者控制台上的一个设置问题。 p>在 APIs& auth 设置您需要授权您想要访问的API,否则结果var中将不会返回任何结果。

如果您检查了它,会看到来自Google的错误消息,告诉您授权API。



从这里添加以下API: APIs& auth> APIs ,选择 Google+ API ,这将返回结果字典与您正在寻找的值。

  from authomatic.providers import openid,oauth2 

CONFIG = {
'oi':{

OpenID提供程序依赖于python -openid包。
'class_':openid.OpenID,
},
'google':{
'class_':oauth2.Google,
'consumer_key':'GOOGLE DEVELOPER客户端ID',
'consumer_secret':'GOOGLE DEVELOPER CLIENT SECRET',
'scope':['profile','email']
}
}

在您的index.html上您将要触发这样的调用:

 < a href =login / googlemethod =GET>使用Google登录< / a> 

这应该触发对google oauth登录的呼叫。

  @ app.route('/ login /< provider_name> /',methods = ['GET','POST'])
def login(provider_name ):

登录处理程序,必须同时接受GET和POST才能使用OpenID


#我们需要响应对象WerkzeugAdapter。
response = make_response()

#将用户登录,将适配器和提供程序名称传递给它。
result = authomatic.login(WerkzeugAdapter(request,response),provider_name)

#如果没有LoginResult对象,登录过程仍然处于等待状态。
如果结果:
如果result.user:
#我们需要更新用户以获取更多信息。
result.user.update()

#其余发生在模板内部。
#result.user.data.x将返回更多用户数据
返回render_template('login.html',email = result.user.email,name = result.user.name)

#不要忘记返回响应。
返回回应


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!

解决方案

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

这篇关于Google和Twitter在Flask上返回空的用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆