REST APIにアクセスしようとしていて、フィルタ条件としてXML行で呼び出す必要があります。他人がアクセスできないコードを提供して謝罪します。このコードを実行すると、以下のエラーメッセージが表示されます。Pythonでurllibを使用してXMLをHTTPにポストする際の問題
import urllib2
import urllib
import hashlib
import hmac
import time
import random
import base64
def MakeRequest():
url = 'https://api01.marketingstudio.com/API/Gateway/9'
publickey = ''
privatekey = ''
method = 'Query'
nonce = random.randrange(123400, 9999999)
age = int(time.time())
final = str(age) + '&' + str(nonce) + '&' + method.lower() + '&' + url.lower()
converted = hmac.new(privatekey, final, hashlib.sha1).digest()
authorization = 'AMS ' + publickey + ':' + base64.b64encode(converted)
xml_string = "<list><FilterItems><FilterItem attribute='pageNumber' value='1'/></FilterItems></list>"
form = {'XML':xml_string}
data = urllib.urlencode(form)
headers = {'Content-Type': 'application/xml'}
req = urllib2.Request(url,data,headers)
req.add_header('ams-method', method)
req.add_header('ams-nonce', nonce)
req.add_header('ams-age', age)
req.add_header('Authorization', authorization)
r = urllib2.urlopen(req)
print r.read()
MakeRequest();
ここにエラーメッセージがあります。
Data at the root level is invalid. Line 1, position 1.
at Aprimo.REST.Core.RESTService.GetRequest(String URI, HttpRequest req)
at Aprimo.REST.RESTHandler.GetRequest(String apiUrl, HttpContext context)
at Aprimo.REST.RESTHandler.ProcessRequest(HttpContext context)
これは正しいロジックとフィルタ条件を持っていると思いますが、これを動作させるにはどうすればよいでしょうか。ありがとう。マークの提案@パー
私は、XML文字列のためでurlencodeを削除して、次のトレースバックを得た:
Traceback (most recent call last):
File "file.py", line 36, in <module>
MakeRequest();
File "file.py", line 32, in MakeRequest
r = urllib2.urlopen(req)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 126, in urlopen
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 392, in open
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 410, in _open
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 370, in _call_chain
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1194, in https_open
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1155, in do_open
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 941, in request
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 975, in _send_request
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 937, in endheaders
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 801, in _send_output
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 773, in send
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 207, in sendall
TypeError: unhashable type
このエラーメッセージはサーバーから返されますか? .NET XML解析エラーです。私はあなたのxml_stringが好きではないリモートサーバーを推測しています。あなたはそれをurlencodingする必要がありますか? – Mark
@マーク私はurlencodeを削除し、別のエラーが出ました:TypeError:unhashable type。私は上記のトレースバック全体を投稿しました。何かアドバイス? – analyticsPierce
あなたはサービスが期待していることに関するドキュメントを持っていますか?どのように元々持っていたのですか?ヘッダーを 'applicaiton/xml'に設定しましたが、xmlは通常 'application/x-www-form-urlencoded'という形式でエンコードされています。この実装の多くは、期待するものに依存しています。それは、私は "unhashable型"についてはわからない、私はちょうどヘッダー= {'Content-Type': 'application/xml'}で要求をフォーマットしようとしました。 req = urllib2.Request(url、xml_string、headers)エラーが発生しなかった – Mark