我正在嘗試在客戶端執行自省,將 Okta 作為我的授權服務器,但不斷收到錯誤
{"error": "missing_authorization", "error_description": "Missing \"Authorization\" in headers."}
我的實作
class MyIntrospectTokenValidator(IntrospectTokenValidator):
def introspect_token(self, token_string):
print(f"Introspecting token {token_string}")
url = f'{okta_keys.get("base_url")}/v1/introspect'
data = {'token': token_string, 'token_type_hint': 'access_token'}
auth = (okta_keys.get('client_id'), okta_keys.get('client_secret'))
resp = requests.post(url, headers=headers, data=data, auth=auth)
resp.raise_for_status()
return resp.json()
require_oauth = ResourceProtector()
require_oauth.register_token_validator(MyIntrospectTokenValidator())
okta = oauth.register(
name='okta',
client_id=secrets["internal_client_id"],
client_secret=secrets["internal_client_secret"],
access_token_url=f'{okta_keys.get("base_url")}/v1/token',
authorize_url=f'{okta_keys.get("base_url")}/v1/authorize',
api_base_url=f'{okta_keys.get("base_url")}',
introspect=f'{okta_keys.get("base_url")}/v1/introspect',
jwks_uri=f'{okta_keys.get("base_url")}/v1/keys',
userinfo_endpoint=f'{okta_keys.get("base_url")}/v1/userinfo',
client_kwargs={'scope': 'openid email profile'},
)
@app.route('/authorize', methods=["GET", "POST"])
def authorize():
_okta = oauth.create_client('okta') # create the google oauth client
token = _okta.authorize_access_token() # Access token from google (needed to get user info)
session.permanent = True # make the session permanant so it keeps existing after broweser gets closed
headers = {'Authorization': f'Bearer {token.get("access_token")}'}
print(f"\n\n{headers}\n\n")
return redirect(url_for('index', _external=True))
@app.route('/oauth/hello-world-api', methods=["GET", "POST"])
@require_oauth(['openid', 'email', 'profile'])
def hello_world():
return str('Hello World')
我一直試圖解決這個問題,但未能成功
- Authlib版本 1.0.0a1
uj5u.com熱心網友回復:
我發現了代碼的問題,我只需要手動向我的 api 提供授權
這是代碼
@app.route('/authorize', methods=["GET", "POST"])
def authorize():
_okta = oauth.create_client('okta') # create the google oauth client
token = _okta.authorize_access_token() # Access token from google (needed to get user info)
session.permanent = True # make the session permanant so it keeps existing after broweser gets closed
headers = {'Authorization': f'Bearer {token.get("access_token")}'}
url = url_for('hello_world', _external=True)
r = requests.get(url, headers=headers)
return redirect(url_for('index', _external=True))
之后,我執行了Postman 的post 請求。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/391666.html
