簡體   English   中英

將python與suds用於sharepoint時的錯誤請求

[英]bad request when using python with suds for sharepoint

我正在使用Suds通過soap訪問Sharepoint列表,但我在使用格式錯誤的肥皂時遇到了一些麻煩。

我使用以下代碼:

from suds.client import Client
from suds.sax.element import Element
from suds.sax.attribute import Attribute
from suds.transport.https import WindowsHttpAuthenticated

import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger('suds.client').setLevel(logging.DEBUG)

ntlm = WindowsHttpAuthenticated(username='somedomain\\username', password='password')
url = "http://somedomain/sites/somesite/someothersite/somethirdsite/_vti_bin/Lists.asmx?WSDL"

client = Client(url, transport=ntlm)

result = client.service.GetListCollection()
print repr(result)

每次我運行它,我得到結果Error 400 Bad request。 當我啟用調試時,我可以看到生成的信封:

<SOAP-ENV:Envelope xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://schemas.microsoft.com/sharepoint/soap/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <ns0:Body>
      <ns1:GetListCollection/>
   </ns0:Body>
</SOAP-ENV:Envelope>

...出現此錯誤消息:

DEBUG:suds.client:http failed:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request</h2>
<hr><p>HTTP Error 400. The request is badly formed.</p>
</BODY></HTML>

通過SoapUI運行相同的WSDL(以及原始包絡數據),請求將按預期返回值。 任何人都可以看到任何明顯的原因,為什么我得到不同的結果與Suds作為SoapUI以及我如何糾正這個?

更新:在不同的Sharepoint站點上測試完全相同的代碼(即不是名稱中有空格的子子站點)和Java(JAX-WS,但同樣的站點也存在問題,但是,不同的問題)看起來好像它按預期工作。 結果我想知道這兩個問題中的一個是否可能是造成這些問題的原因:

  • SOAP實現在Sharepoint中的subsubsubsites有一些問題嗎?
  • SOAP實現在名稱中有一些空白問題,即使使用%20作為替代?

我仍然需要將原始URL與這些問題一起使用,因此任何輸入都將受到高度贊賞。 我假設因為SoapUI使用原始URL,所以應該可以糾正任何錯誤。

我認為我縮小了問題范圍,它特定於suds(可能還有其他SOAP實現)。 你的要點:

  • SOAP實現在名稱中有一些空白問題,即使使用%20作為替代?

那就是現場。 打開suds的調試日志記錄使我能夠獲取端點,信封和標題。 使用cURL模仿完全相同的調用會返回一個有效的響應,但suds會拋出錯誤的請求。

問題是suds接受你的WSDL(url參數)並解析它,但它不包括URL編碼的字符串。 這導致調試消息如下:

DEBUG:suds.transport.http:opening (https://sub.site.com/sites/Site Collection with Spaces/_vti_bin/UserGroup.asmx?WSDL)
<snip>
TransportError: HTTP Error 400: Bad Request

通過fiddler代理管理此請求表明它正在運行針對URL https://sub.site.com/sites/Site的請求,因為它解析了WSDL。 問題是您沒有將location參數傳遞給suds.client.Client。 以下代碼每次都給我有效的回復:

from ntlm3 import ntlm
from suds.client import Client
from suds.transport.https import WindowsHttpAuthenticated

# URL without ?WSDL
url = 'https://sub.site.com/sites/Site%20Collection%20with%20Spaces/_vti_bin/Lists.asmx'

# Create NTLM transport handler
transport = WindowsHttpAuthenticated(username='foo',
                                     password='bar')
# We use FBA, so this forces it to challenge us with 
# a 401 so WindowsHttpAuthenticated can take over.
msg = ("%s\\%s" % ('DOM', 'foo'))
auth = 'NTLM %s' % ntlm.create_NTLM_NEGOTIATE_MESSAGE(msg).decode('ascii')
# Create the client and append ?WSDL to the URL.
client = Client(url=(url + "?WSDL"),
                location=url,
                transport=transport)
# Add the NTLM header to force negotiation.
header = {'Authorization': auth}
client.set_options(headers=header)

一個警告:使用urllib quote可以工作,但是您無法對整個URL進行編碼,或者無法識別URL。 你最好只用%20替換空格。

url = url.replace(' ','%20')

希望這可以防止別人撞到牆上。

暫無
暫無

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

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