繁体   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