我有一个python API(带有oAuth 2.0的Eve-Flask)在localhost中成功运行。
我已将此应用程序部署到heroku,并且在获取令牌密钥后无法访问API资源,但是我可以使用生成的相同令牌访问某个端点。
你有:
> curl -k -X POST -d "client_id=XXXXX&grant_type=password&username=XXX&password=YYYY" https://MYAPP.herokuapp.com/oauth/token
api响应是:
> {"access_token": "VWQjYBOMTkeqPbiql8dl2T1fbBM1WH", "token_type": "Bearer", "expires_in": 3600, "refresh_token": "Z0d1O3LUVrE1lIaYuQ3hOkZSFO9GuX", "scope": ""}
到目前为止,一切都是正确的,除此之外,我可以使用生成的令牌访问某个端点:
> curl -k -H "Authorization: Bearer VWQjYBOMTkeqPbiql8dl2T1fbBM1WH" https://MYAPP.herokuapp.com/myendpoint
>You have access the protected resource!
另一方面:
curl -k -H "Authorization: Bearer VWQjYBOMTkeqPbiql8dl2T1fbBM1WH" https://MYAPP.herokuapp.com/api_resource
Traceback (most recent call last):
File "/app/.heroku/python/lib/python2.7/site-packages/eve/flaskapp.py", line 968, in __call__
return super(Eve, self).__call__(environ, start_response)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/app/.heroku/python/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/.heroku/python/lib/python2.7/site-packages/eve/endpoints.py", line 54, in collections_endpoint
response = get(resource, lookup)
File "/app/.heroku/python/lib/python2.7/site-packages/eve/methods/common.py", line 242, in rate_limited
return f(*args, **kwargs)
File "/app/.heroku/python/lib/python2.7/site-packages/eve/auth.py", line 77, in decorated
if not auth.authorized(roles, resource_name, request.method):
File "/app/oauth2.py", line 45, in authorized
return self.check_auth(token, allowed_roles, resource, method)
File "/app/oauth2.py", line 33, in check_auth
return token and self.redis.get(token)
File "/app/.heroku/python/lib/python2.7/site-packages/redis/client.py", line 880, in get
return self.execute_command('GET', name)
File "/app/.heroku/python/lib/python2.7/site-packages/redis/client.py", line 578, in execute_command
connection.send_command(*args)
File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 563, in send_command
self.send_packed_command(self.pack_command(*args))
File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 538, in send_packed_command
self.connect()
File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 442, in connect
raise ConnectionError(self._error_message(e))
ConnectionError: Error 111 connecting to localhost:6379. Connection refused.
pice of run.py
app = Eve(auth=BearerAuth)
ResourceOwnerPasswordCredentials(app)
@app.route('/myendpoint')
@oauth.require_oauth()
def restricted_access():
return "You have access the protected resource!"
if __name__ == '__main__':
host = str(os.environ.get('HOST', '0.0.0.0'))
app.run(host, int(os.environ.get('PORT')),debug=True)
我在heroku中设置了Redis,但是,这个错误:
ConnectionError: Error 111 connecting to localhost:6379. Connection refused.
Eve尝试连接我的redis localhost,我不知道为什么,这个错误很快就会杀了我。
提前致谢。
尼古拉斯
答案 0 :(得分:1)
最后我可以缓解redis错误并在Heroku编辑oauth2.py上运行api:
def __init__(self):
super(BearerAuth, self).__init__()
self.redis = StrictRedis(host='XXX',port=123,password='YYY')
而不是
self.redis = StrictRedis()
干杯。
答案 1 :(得分:0)
堆栈跟踪表明连接到redis的问题:
File "/app/.heroku/python/lib/python2.7/site-packages/redis/connection.py", line 442, in connect
raise ConnectionError(self._error_message(e))
所以我会确保redis在您的实例上启动并运行。还要确保SENTINEL_REDIS_URL
设置为正确的端口(也许heroku在非标准端口上运行redis?)