我不明白出了什么问题? 回调uri:
google - http://127.0.0.1:5000/auth/google/
twitter - http://127.0.0.1:5000/auth/tw/
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': '####',
},
}
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
在我接受谷歌或推特上的应用访问后,我已经重定向到带有闪光按摩的index.html页面&#34;您的提供商返回空数据,稍后再试&#34;
在控制台中我看到: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 -
如果我得到302,可能有些不对劲 - 在回复???
请帮助我!
答案 0 :(得分:2)
我认为这是Google Developer Console上的设置问题。
在 API&amp;您需要授权要访问的API的项目的auth 设置,否则将不会在结果var中返回任何内容。
如果您检查它,您会看到来自Google的错误消息,告知您授权API。
从这里添加以下API: API&amp; auth&gt; API ,选择 Google+ API ,这将返回包含您要查找的值的结果字典。
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']
}
}
你的index.html上的你想要触发这样的调用:
<a href="login/google" method="GET">Sign In With Google</a>
这应该触发对谷歌oauth登录的调用。
@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