Golang与SteamAPI结合开发指南
Golang与SteamAPI结合开发指南 大家好! 请告诉我如何创建一个简单的Go语言应用程序来连接Steam商店,并获取某个游戏或指定游戏中产品的信息和价格? go-steam
以及doc.go文件中的简单示例 go-steam/doc
但我不确定连接后需要使用哪些方法? 非常感谢!
是的,很抱歉 物品交易 或游戏成本
看起来那个API并不提供Steam商店的信息。你所说的选定游戏中的产品是什么意思?是指物品交易吗?
对于同一个应用ID存在多种不同的价格,从基础游戏到DLC和游戏套装。您是否只需要基础游戏的价格信息?
根据您需要获取的价格数据类型,必须使用一些特殊技巧才能通过Steam API获取正确的价格信息。
我需要获取游戏信息和价格
所有游戏列表我通过请求 http://api.steampowered.com/ISteamApps/GetAppList/v2 获取
现在我需要通过ID查找游戏价格
我找到了URL https://store.steampowered.com/api/appdetails?appids=gameid
但是返回的JSON数据非常庞大 - 我需要更简单的方式,这样我就不用在Golang中创建庞大的结构体
只需要获取美元或欧元价格
是的,我只需要基础游戏的价格信息 我找到了一些解决方案,使用 类似这样的请求
https://store.steampowered.com/api/appdetails/?appids=237110&cc=us&filters=price_overview
但返回的JSON看起来像这样
{ “237110”: { “success”: true, “data”: { “price_overview”: { “currency”: “USD”, “initial”: 1999, “final”: 1999, “discount_percent”: 0 } } } }
在Go结构体中它看起来像这样
type AutoGenerated struct { Num237110 struct { Success bool
json:"success"Data struct { PriceOverview struct { Currency stringjson:"currency"Initial intjson:"initial"Final intjson:"final"DiscountPercent intjson:"discount_percent"}json:"price_overview"}json:"data"}json:"237110"}
但是在Golang中如何正确定义适用于这种JSON的通用结构体呢? 谢谢!
要使用Go语言连接Steam商店API并获取游戏信息,可以使用go-steam库。以下是一个完整的示例,展示如何连接Steam网络并查询产品信息:
package main
import (
"log"
"time"
"github.com/Philipp15b/go-steam"
"github.com/Philipp15b/go-steam/protocol/steamlang"
"github.com/Philipp15b/go-steam/steamid"
)
func main() {
// 创建Steam客户端配置
client := steam.NewClient()
// 连接到Steam网络
if err := client.Connect(); err != nil {
log.Fatalf("连接失败: %v", err)
}
defer client.Disconnect()
// 设置事件处理器
client.Events().On(steam.ConnectedEvent{}, func(ev interface{}) {
log.Println("已连接到Steam网络")
// 匿名登录(用于公开数据访问)
client.Auth.LogOnAnonymous()
})
client.Events().On(steam.LoggedOnEvent{}, func(ev interface{}) {
log.Println("匿名登录成功")
// 查询游戏信息 - 以CS:GO为例(AppID: 730)
appID := uint32(730)
packageID := uint32(0) // 0表示查询基础游戏
// 发送产品信息请求
client.Social.SendProductInfoRequest(appID, packageID)
})
client.Events().On(steam.ProductInfoEvent{}, func(ev interface{}) {
productInfo := ev.(*steam.ProductInfoEvent)
log.Printf("收到产品信息 - AppID: %d", productInfo.AppID)
// 解析产品信息
if productInfo.Result == steamlang.EResult_OK {
log.Printf("产品名称: %s", productInfo.AppInfo.Common.Name)
log.Printf("产品类型: %s", productInfo.AppInfo.Common.GameType)
log.Printf("元数据状态: %v", productInfo.AppInfo.Common.MetadataState)
// 如果有价格信息
if productInfo.AppInfo.Common.StoreTags != nil {
log.Printf("商店标签: %v", productInfo.AppInfo.Common.StoreTags)
}
} else {
log.Printf("查询失败: %v", productInfo.Result)
}
})
client.Events().On(steam.DisconnectedEvent{}, func(ev interface{}) {
log.Println("与Steam断开连接")
})
// 保持连接运行
time.Sleep(30 * time.Second)
}
对于更具体的价格查询,可以使用Steam商店API:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
// Steam价格响应结构体
type PriceOverview struct {
Success bool `json:"success"`
Currency string `json:"currency"`
Initial int `json:"initial"`
Final int `json:"final"`
Discount int `json:"discount_percent"`
PriceFormatted string `json:"final_formatted"`
}
func getSteamPrice(appID string) {
url := fmt.Sprintf("https://store.steampowered.com/api/appdetails?appids=%s&cc=us&filters=price_overview", appID)
resp, err := http.Get(url)
if err != nil {
log.Fatalf("HTTP请求失败: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("读取响应失败: %v", err)
}
var result map[string]map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
log.Fatalf("JSON解析失败: %v", err)
}
appData, exists := result[appID]
if !exists || !appData["success"].(bool) {
log.Println("未找到应用数据")
return
}
data := appData["data"].(map[string]interface{})
if priceOverview, exists := data["price_overview"]; exists {
priceBytes, _ := json.Marshal(priceOverview)
var price PriceOverview
json.Unmarshal(priceBytes, &price)
fmt.Printf("游戏价格: %s\n", price.PriceFormatted)
fmt.Printf("原价: %d 分\n", price.Initial)
fmt.Printf("折扣: %d%%\n", price.Discount)
fmt.Printf("货币: %s\n", price.Currency)
}
}
func main() {
// 查询CS:GO价格(AppID: 730)
getSteamPrice("730")
// 查询Dota 2价格(AppID: 570)
getSteamPrice("570")
}
运行前需要安装依赖:
go mod init steam-app
go get github.com/Philipp15b/go-steam
第一个示例使用go-steam库直接连接Steam网络获取产品信息,第二个示例使用Steam商店公开API获取价格信息。根据你的具体需求选择合适的方法。


