通过 heroku 使用 oAuth 设置 Eve-Flask

Setup Eve-Flask with oAuth over heroku

我有一个 python API(带有 oAuth 2.0 的 Eve-Flask)运行 在本地主机中成功。

我已经将此应用程序部署到 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.

张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.

前夕尝试连接我的 redis 本地主机,我不知道为什么,这个错误很快就会杀死我。

提前致谢。

尼古拉斯

堆栈跟踪表明连接到 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 在 non-standard 端口上运行 redis?)

我终于可以减轻 Redis 错误和 运行 api over Heroku 编辑 oauth2.py:

def __init__(self):
        super(BearerAuth, self).__init__()
        self.redis = StrictRedis(host='XXX',port=123,password='YYY')

而不是

self.redis = StrictRedis()

干杯。