我正在 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
部分。也不要使用 urllib.parse
,因为它会将参数名称(例如“itemid[0]”)更改为“itemid%5B0%5D”,这在 steam 端将无法正确解析。
最终代码示例:
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
pid = "testItem1"
appid = "480"
itemcount = 1
currency = 'CNY'
amount = 350
language = 'zh-CN'
description = 'testing_description'
urlSandbox = "/ISteamMicroTxnSandbox/"
s = f'{urlSandbox}InitTxn/v3/?key={key}&orderid={orderid}&appid={appid}&steamid={steamid}&itemcount={itemcount}&language={language}¤cy={currency}&itemid[0]={pid}&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())
我以为我可以以正确的格式发送 HTTPs 请求,但我仍然收到 403 错误
b'<html><head><title>Forbidden</title></head><body><h1>Forbidden</h1>Access is denied. Retrying will not help. Please verify your <pre>key=</pre> parameter.</body></html>'