Я совершаю внутриигровую покупку своей игры в Steam. На своем сервере я использую Python 3. Я пытаюсь сделать https-запрос следующим образом:
conn = http.client.HTTPSConnection("partner.steam-api.com")
orderid = uuid.uuid4().int & (1<<64)-1
print("orderid = ", orderid)
key = "xxxxxxxxxxxxxxxxxxx" # omitted for security reason
steamid = "xxxxxxxxxxxxxxxxxxx" # omitted for security reason
pid = "testItem1"
appid = "480"
itemcount = 1
currency = 'CNY'
amount = 350
description = 'testing_description'
urlSandbox = "/ISteamMicroTxnSandbox/"
s = f'{urlSandbox}InitTxn/v3/?key = {key}&orderid = {orderid}&appid = {appid}&steamid = {steamid}&itemcount = {itemcount}¤cy = {currency}&itemid[0] = {pid}&qty[0] = {1}&amount[0] = {amount}&description[0] = {description}'
print("s = ", s)
conn.request('POST', s)
r = conn.getresponse()
print("InitTxn result = ", r.read())
Я проверил s в консоли, а именно:
s = /ISteamMicroTxnSandbox/InitTxn/v3/?key=xxxxxxx&orderid=11506775749761176415&appid=480&steamid=xxxxxxxxxxxx&itemcount=1¤cy=CNY&itemid[0]=testItem1&qty[0]=1&amount[0]=350&description[0]=testing_description
Однако я получил плохой ответ на запрос:
InitTxn result = b"<html><head><title>Bad Request</title></head><body><h1>Bad Request</h1>Required parameter 'orderid' is missing</body></html>"
Как это решить? Спасибо!
Кстати, я использую почти тот же способ вызова GetUserInfo, за исключением изменения параметров и замены POST запросом GET, и это работает хорошо.
Только что прочитал, что мне нужно указать параметры в сообщении. Поэтому я изменил коды следующим образом, но все равно получаю ту же ошибку: «Необходимый параметр orderid отсутствует»
params = {
'key': key,
'orderid': orderid,
'appid': appid,
'steamid': steamid,
'itemcount': itemcount,
'currency': currency,
'pid': pid,
'qty[0]': 1,
'amount[0]': amount,
'description[0]': description
}
s = urllib.parse.urlencode(params)
# In console: s = key=xxxxx&orderid=9231307508782239594&appid=480&steamid=xxx&itemcount=1¤cy=CNY&pid=testItem1&qty%5B0%5D=1&amount%5B0%5D=350&description%5B0%5D=testing_description
print("s = ", s)
conn.request('POST', url=f'{urlSandbox}InitTxn/v3/', body=s)
==== обновление ====
Проблема с форматом решена. Пожалуйста, смотрите ответ ниже.






Я пропустил часть content-type в заголовках и языке. Также itemid должен быть uint32.
Окончательный пример кода:
conn = http.client.HTTPSConnection("partner.steam-api.com")
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
orderid = uuid.uuid4().int & (1<<64)-1
print("orderid = ", orderid)
key = "xxxxxxxxxxxxxxxxxxx" # omitted for security reason
steamid = "xxxxxxxxxxxxxxxxxxx" # omitted for security reason
itemid = 100001
appid = "480"
itemcount = 1
currency = 'CNY'
amount = 350
language = 'zh-CN'
description = 'testing_description'
urlSandbox = "/ISteamMicroTxnSandbox/"
s = f'key = {key}&orderid = {orderid}&appid = {appid}&steamid = {steamid}&itemcount = {itemcount}&language = {language}¤cy = {currency}&itemid[0] = {itemid}&qty[0] = {1}&amount[0] = {amount}&description[0] = {description}'
conn.request('POST', url=f'{urlSandbox}InitTxn/v3/', headers=headers, body=s)
r = conn.getresponse()
print("InitTxn result = ", r.read())