簡體   English   中英

如何訪問經過 PingIdentity 認證的 API; 通過 python?

[英]How to hit a API which is PingIdentity authenticated; via python?

我有這個 API,是這樣的:

https://baseurl.com/endpoint

它是一個 GET API 並具有 PingIdentity 的 OIDC + Auth2.0 身份驗證和授權(在 KONG API GATEWAY 級別啟用)機制。 The first time I hit this API via my browser, it redirects me to a sign-in page, which on successful sign-in, successfully triggers this API and shows me the output JSON on the browser. 在接下來的 1 小時內,每當我再次點擊此 API 時,它都不會再次要求登錄。 之后,我再次必須登錄一次。

現在,我需要通過 Python 訪問這個 API。 問題是,作為響應,它給出了 HTML output,這基本上是瀏覽器將我重定向到的登錄頁面。 這是我嘗試過的:

我在 python 中使用 FastAPI 編寫了這個 API,當我在瀏覽器上請求它時,我通過request.headers在 FastAPI 中記錄了它的標題。 以下是標題包含的內容:

'host':
'keep-alive':
'connection':
'x-forwarded-for':
'x-forwarded-proto':
'x-forwarded-host':
'x-forwarded-port':
'x-forwarded-path':
'x-forwarded-prefix':
'x-real-ip':
'cache-control':
'sec-ch-ua':
'sec-ch-ua-mobile':
'sec-ch-ua-platform':
'upgrade-insecure-requests':
'user-agent':
'accept':
'sec-fetch-site':
'sec-fetch-mode':
'sec-fetch-user':
'sec-fetch-dest':
'referer':
'accept-encoding':
'accept-language':
'cookie': {contained my organization specific data, which I am sure are not essential}
'username':
'client_id':
'oidc-access-token': {it is a JWT token, which I have confirmed with my teammates, is the access token from PingIdentity}

但是,當我在使用 Python 請求庫時設置這些相同的標頭以訪問相同的 API 時,它再次返回給我 HTML 的標頭,我也嘗試了登錄頁面的 WORKS 結果,而不是從標頭中給我瀏覽器中的調試器工具並在 python 的請求中設置相同的參數:但仍然沒有任何效果! 這是我在 python 中擊中 API 的方式:

import requests
hit_my_api = requests.get("https://baseurl.com/endpoint", headers={<The ones I mentioned above>})

如何解決這個問題?

您應該在如下標題中添加 JWT 令牌(請參見此處):

hit_my_api = requests.get("https://baseurl.com/endpoint", headers={'Authorization': 'access_token myToken'})

編輯:如果上述方法不起作用,請嘗試以下操作:

hit_my_api = requests.get("https://baseurl.com/endpoint", headers={ 'Authorization': 'Bearer <your_token>' })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM