Python 中的 FIWARE 身份验证
FIWARE Authentication in Python
我正在尝试使用 FIWARE 对用户进行身份验证。当我请求令牌时它 returns 是 404,但我在获取访问代码请求时没有问题。
我的代码:
class OAuth2(object):
def __init__(self):
self.client_id = "<client_id>"
self.client_secret = "<client_secret>"
self.site = 'http://0.0.0.0:8000'
self.redirect_uri = "http://192.168.99.101:8000/auth"
self.authorization_url = '/oauth2/authorize'
self.token_url = '/oauth2/token'
def authorize_url(self, **kwargs):
oauth_params = {'response_type': 'code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id}
oauth_params.update(kwargs)
return "%s%s?%s" % (self.site, quote(self.authorization_url), urlencode(oauth_params))
def get_token(self, code, **kwargs):
url = "%s%s" % (self.site, quote(self.token_url))
data = {'grant_type': 'authorization_code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code}
data.update(kwargs)
response = requests.post(url, data=data)
content = response.content
if isinstance(response.content, str):
try:
content = json.loads(response.content)
except ValueError:
content = parse_qs(response.content)
else:
return content
在我的应用程序中,我调用 authorize_url() 来获取代码。
@app.route("/authenticate")
def authenticate():
auth_url = auth_app.authorize_url()
return redirect(auth_url)
之后,我通过回调 url 获取代码并调用 get_token() 方法:
@app.route('/auth', methods=['GET', 'POST'])
def auth():
error = request.args.get('error', '')
if error:
return "Error: " + error
code = request.args.get('code')
content = auth_app.get_token()
return render_template('index.html', content="content: " + content)
Github 项目:https://github.com/I-am-Gabi/security-app/tree/master/2-BasicAuthentication/securityapp-ui/web
OAuth2 class: https://github.com/I-am-Gabi/security-app/blob/master/2-BasicAuthentication/securityapp-ui/web/oauth_fiware.py
固件维基:https://github.com/ging/fiware-idm/wiki/using-the-fiware-lab-instance
尝试使用 POST 方法而不是 GET
请检查您是否正确发送授权 header
我正在尝试使用 FIWARE 对用户进行身份验证。当我请求令牌时它 returns 是 404,但我在获取访问代码请求时没有问题。 我的代码:
class OAuth2(object):
def __init__(self):
self.client_id = "<client_id>"
self.client_secret = "<client_secret>"
self.site = 'http://0.0.0.0:8000'
self.redirect_uri = "http://192.168.99.101:8000/auth"
self.authorization_url = '/oauth2/authorize'
self.token_url = '/oauth2/token'
def authorize_url(self, **kwargs):
oauth_params = {'response_type': 'code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id}
oauth_params.update(kwargs)
return "%s%s?%s" % (self.site, quote(self.authorization_url), urlencode(oauth_params))
def get_token(self, code, **kwargs):
url = "%s%s" % (self.site, quote(self.token_url))
data = {'grant_type': 'authorization_code', 'redirect_uri': self.redirect_uri, 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code}
data.update(kwargs)
response = requests.post(url, data=data)
content = response.content
if isinstance(response.content, str):
try:
content = json.loads(response.content)
except ValueError:
content = parse_qs(response.content)
else:
return content
在我的应用程序中,我调用 authorize_url() 来获取代码。
@app.route("/authenticate")
def authenticate():
auth_url = auth_app.authorize_url()
return redirect(auth_url)
之后,我通过回调 url 获取代码并调用 get_token() 方法:
@app.route('/auth', methods=['GET', 'POST'])
def auth():
error = request.args.get('error', '')
if error:
return "Error: " + error
code = request.args.get('code')
content = auth_app.get_token()
return render_template('index.html', content="content: " + content)
Github 项目:https://github.com/I-am-Gabi/security-app/tree/master/2-BasicAuthentication/securityapp-ui/web
OAuth2 class: https://github.com/I-am-Gabi/security-app/blob/master/2-BasicAuthentication/securityapp-ui/web/oauth_fiware.py
固件维基:https://github.com/ging/fiware-idm/wiki/using-the-fiware-lab-instance
尝试使用 POST 方法而不是 GET
请检查您是否正确发送授权 header