Steamworks Web API ConsumeItem返回空JSON且物品未删除问题
问题:Steamworks Web API ConsumeItem返回空JSON,物品未被删除
我正在搭建一个物品兑换系统,逻辑是回收玩家低稀有度物品以发放高稀有度物品,但调用Steam的ConsumeItem接口时,Steam返回空JSON,且目标物品并未从玩家库存中删除。以下是我的服务器端代码:
private async Task<bool> RemoveItemsFromInventory(string steamId, List<int> itemIds) { var content = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("key", ItemPurchase.steamWebApiKey), new KeyValuePair<string, string>("steamid", steamId), new KeyValuePair<string, string>("appid", "3081680"), }; for (int i = 0; i < itemIds.Count; i++) { content.Add(new KeyValuePair<string, string>($"itemid[{i}]", itemIds[i].ToString())); content.Add(new KeyValuePair<string, string>($"quantity[{i}]", "1")); } foreach (var pair in content) { Console.WriteLine($"{pair.Key}: {pair.Value}"); } var encoded = new FormUrlEncodedContent(content); var response = await client.PostAsync($"https://api.steampowered.com/IInventoryService/ConsumeItem/v1", encoded); string responseContent = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { Console.WriteLine("Request successful."); Console.WriteLine("Response: " + responseContent); return true; } else { Console.WriteLine("Request failed with status code: " + response.StatusCode); Console.WriteLine("Response: " + responseContent); return false; } }
排查与解决方案
1. 缺失必填参数contextid
ConsumeItem接口必须指定contextid参数,它用于标识物品所在的库存上下文(游戏内物品常用默认值为2)。缺少该参数会导致Steam无法定位目标物品,直接返回空响应。
需在参数列表中添加:
new KeyValuePair<string, string>("contextid", "2"),
2. itemid类型错误(64位整数截断)
Steam物品的实例ID是64位整数,代码中用List<int>存储itemIds会导致数值溢出(int最大值为2^31-1,远小于Steam的itemid范围),传送给Steam的是错误ID,自然无法找到物品。
需将List<int> itemIds修改为List<long> itemIds,同时更新循环内的ToString逻辑:
private async Task<bool> RemoveItemsFromInventory(string steamId, List<long> itemIds) { // ... 其他代码不变 content.Add(new KeyValuePair<string, string>($"itemid[{i}]", itemIds[i].ToString())); // ... }
3. 验证参数正确性
- 确认
steamid是完整的64位SteamID字符串,无格式错误。 - 检查Web API密钥已正确关联目标AppID(3081680),且拥有
IInventoryService的调用权限。 - 确保传入的
itemid是玩家库存中实际存在的物品实例ID,而非物品定义ID(defid)。
修改后的完整代码示例
private async Task<bool> RemoveItemsFromInventory(string steamId, List<long> itemIds) { var content = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("key", ItemPurchase.steamWebApiKey), new KeyValuePair<string, string>("steamid", steamId), new KeyValuePair<string, string>("appid", "3081680"), new KeyValuePair<string, string>("contextid", "2"), // 新增必填参数 }; for (int i = 0; i < itemIds.Count; i++) { content.Add(new KeyValuePair<string, string>($"itemid[{i}]", itemIds[i].ToString())); content.Add(new KeyValuePair<string, string>($"quantity[{i}]", "1")); } foreach (var pair in content) { Console.WriteLine($"{pair.Key}: {pair.Value}"); } var encoded = new FormUrlEncodedContent(content); var response = await client.PostAsync($"https://api.steampowered.com/IInventoryService/ConsumeItem/v1", encoded); string responseContent = await response.Content.ReadAsStringAsync(); if (response.IsSuccessStatusCode) { Console.WriteLine("Request successful."); Console.WriteLine("Response: " + responseContent); return true; } else { Console.WriteLine("Request failed with status code: " + response.StatusCode); Console.WriteLine("Response: " + responseContent); return false; } }
内容的提问来源于stack exchange,提问作者hapupich
相关产品推荐
相关产品推荐

