简体   繁体   English

Python脚本获取会话票证

[英]Python script to get Session Ticket

I have a service, that exposes API for logging in. In my browser if I do 我有一项服务,该服务公开了用于登录的API。

<host>:<port>/myservice/api/login?u=admin&pw=admin

The above url, returns a ticket, that I can pass along for my successive requests. 上面的url返回一个票证,我可以将其传递给后续的请求。

More details about the same, here. 有关详细信息,请参见此处。

Below is my python script. 以下是我的python脚本。

import urllib

url = 'http://<host>:<port>/myservice/api/login?u=admin&pw=admin'
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
print data

When I run this I get 当我运行这个我得到

IOError: ('http error', 401, 'Authorization Required', <httplib.HTTPMessage instance at 0x<somenumber>>)

Now, I am not sure what am I supposed to do. 现在,我不确定该怎么办。 So I went to my browser, and opened the developer's console. 因此,我转到浏览器,并打开了开发人员的控制台。

Apparently, the url has moved to something else. 显然,URL已移至其他位置。 I see two requests. 我看到两个请求。

first one hits the url that I am hitting. 第一个点击我正在点击的网址。 Response Header has a Location:Parameter. 响应标头具有Location:Parameter。

The second request hits the url that is returned as Location parameter. 第二个请求将命中作为Location参数返回的URL。 the Authorization header has 'Negotiation 授权标头具有“协商”

It also has a setcookie in the response header. 在响应头中也有一个setcookie。

Now, I am not sure what exactly to do with this information, but if someone can help. 现在,我不确定该信息到底是怎么做的,但不确定是否有人可以提供帮助。 Thanks 谢谢

I believe you problem is having the wrong URL for the login service 我相信您的问题是登录服务的URL错误

If I change you code to instead be: 如果我将您的代码更改为:

import urllib, json

url = 'http://localhost:8080/alfresco/service/api/login?u=admin&pw=admin&format=json'
print "Retrieving %s" % url
uh = urllib.urlopen(url)
data = uh.read()
print "Retrieved %d characters" %  len(data)
print "Data is %s" % data

ticket = json.loads(data)["data"]["ticket"]
print "Ticket is %s" % ticket

Then against a freshly installed Alfresco 4.2 server, I get back a login Ticket for the admin user. 然后在新安装的Alfresco 4.2服务器上,我找回了管理员用户的登录票证。

Note the use of the json format of the login API - much easier to parse from JSON, and of the correct path to the login api - /alfresco/service/api/login 请注意登录API的json格式的使用-更容易从JSON解析,以及登录api的正确路径- /alfresco/service/api/login

try this two small changes may be it will help: 尝试这两个小的更改可能会有所帮助:

1) use urllib.urlencode while passing parameters to request url 1)在传递参数以请求url时使用urllib.urlencode

import urllib
params = urllib.urlencode({'u': 'admin', 'pw': 'admin'})
uh = urllib.urlopen("http://<host>:<port>/myservice/api/login?%s" % params')

2) Stimulate a web browser while making a request using urllib2 2)在使用urllib2发出请求时刺激Web浏览器

import urllib2
req = urllib2.Request('http://<host>:<port>/myservice/api/login?u=admin&pw=admin', headers={ 'User-Agent': 'Mozilla/5.0' })
uh = urllib2.urlopen(req)

401 is unauthorized error! 401是未经授权的错误! it means you are not authorized to access API. 这表示您无权访问API。 Did you already signup for API keys and access tokens? 您是否已经注册了API密钥和访问令牌?

Check detailed description of 401 Error: 查看401错误的详细说明:

http://techproblems.org/http-error-401/ http://techproblems.org/http-error-401/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM