使用Python requests_kerberos进行API连接

2024-09-19 23:36:09 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我第一次尝试使用Kerberos身份验证通过python(从windows)连接到API。我已经做了几天了,但我的进展停滞了。以下是我使用的一些参考资料:

这是我找到的最好的教程,但它似乎使用了一个不推荐使用的模块,而不是requests\u kerberos: http://python-notes.curiousefficiency.org/en/latest/python_kerberos.html

https://pypi.org/project/requests-kerberos/

https://programtalk.com/python-examples/requests_kerberos.HTTPKerberosAuth/

以下是我迄今为止所做的尝试(我将屏蔽敏感信息):

import requests
from requests_kerberos import HTTPKerberosAuth
r=requests.get("https://apiServer.hadoop.company.com:23232/templeton/v1/ddl/database/",auth=HTTPSKerberosAuth)

以下是一些输出:

^{pr2}$

401

r.headers

{'Content-Length': '1321', 'Set-Cookie': 'hadoop.auth=; Path=/; HttpOnly', 'Server': 'Jetty(7.6.0.v20120127)', 'Cache-Control': 'must-revalidate,no-cache,no-store', 'Content-Type': 'text/html;charset=ISO-8859-1', 'WWW-Authenticate': 'Negotiate'}

r.text

<html>\n<head>\n<meta http-equiv="Content-Type" 
content="text/html;charset=ISO
-8859-1"/>\n<title>Error 401 Authentication 
required</title>\n</head>\n<body>\n<
h2>HTTP ERROR: 401</h2>\n<p>Problem accessing /templeton/v1/ddl/database/. 
Reason:\n<pre>    Authentication required</pre></p>\n<hr /><i><small>Powered 
by Jetty://</small></i>

这些输出是否至少表明服务器收到了我的请求?

如果我使用curl从我们的linux环境连接到API,它就可以正常工作,并且收到预期的输出:

curl --negotiate -i -u :  'http://apiServer.hadoop.company.com:23232/templeton/v1/ddl/database/'

HTTP/1.1 401 Authentication required WWW-Authenticate: Negotiate Set-Cookie: hadoop.auth=; Path=/; HttpOnly Cache-Control: must-revalidate,no-cache,no-store Content-Type: text/html;charset=ISO-8859-1 Content-Length: 1321 Server: Jetty(7.6.0.v20230127)

HTTP/1.1 200 OK WWW-Authenticate: Negotiate YGY1cwVaADAgCAQ+iSTBHoAMCAReiQAQ+sf/nekePw09B/cboDrINa7qn+aENRuw2V+OW7Y7Rk9pOwGa8hrXC3rXKxCk= Set-Cookie: hadoop.auth="u=svc-qa-dsafqa-dev&p=svc-fd-itdflea-dev@hadoop.company.com&t=kerberos&e=15392343251&s=nWk/bFDbHQfsadfewe8PtjAsVHs="; Path=/; HttpOnly Content-Type: application/json Transfer-Encoding: chunked Server: Jetty(7.6.0.v20120127)

我意识到我对kerberos身份验证的基本理解有一个差距,我正试图参加一个速成课程,但我只需要能够连接到这个api。非常感谢任何帮助。在


Tags: notexthttpscomhadoopauthhttphtml
1条回答
网友
1楼 · 发布于 2024-09-19 23:36:09

您需要提供HTTPSKerberosAuth的实例,而不是类本身,因此您的请求应该是:

r = requests.get("https://apiServer.com", auth=HTTPSKerberosAuth())

{cd2>在括号中。在

相关问题 更多 >