如何访问通过PingIdentity身份验证的API;通过python? [英] How to hit a API which is PingIdentity authenticated; via python?

查看:46
本文介绍了如何访问通过PingIdentity身份验证的API;通过python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个接口,大概是这样的:

https://baseurl.com/endpoint

Get接口,具有PingIdentity的OIDC+Auth2.0认证授权(在香港API网关级开启)机制。我第一次通过浏览器点击此API时,它会将我重定向到一个登录页面,在成功登录后,它会成功触发此API并在浏览器上显示输出JSON。在接下来的1个小时里,每当我再次点击此API时,它都不会再次要求登录。之后,我必须再次登录一次。

现在,我需要通过Python访问该接口。问题是,作为响应,它会给出一个HTML输出,这基本上就是浏览器将我重定向到的登录页面。 以下是我尝试过的方法:

我用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,并且不给我结果!我还尝试从浏览器的调试器工具中的网络选项卡中复制标头,并在我的请求中设置相同的参数,但仍然不起作用! 以下是我如何在python中使用API:

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

如何解决此问题?

推荐答案

您应该在Header中添加jwt令牌,如下所示(参见here):

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>' })

这篇关于如何访问通过PingIdentity身份验证的API;通过python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆