Exemple #1
0
func (c *Client) Accept(id TradeOfferId) error {
	baseurl := fmt.Sprintf("https://steamcommunity.com/tradeoffer/%d/", id)
	req := netutil.NewPostForm(baseurl+"accept", netutil.ToUrlValues(map[string]string{
		"sessionid":    c.sessionId,
		"serverid":     "1",
		"tradeofferid": strconv.FormatUint(uint64(id), 10),
	}))
	req.Header.Add("Referer", baseurl)

	resp, err := c.client.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		bts, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return err
		}
		log.Println(string(bts))
		return fmt.Errorf("accept error: status code %d", resp.StatusCode)
	}
	return nil
}
Exemple #2
0
func (c *Client) action(method string, version uint, id TradeOfferId) error {
	resp, err := c.client.Do(netutil.NewPostForm(fmt.Sprintf(apiUrl, method, version), netutil.ToUrlValues(map[string]string{
		"key":          string(c.key),
		"tradeofferid": strconv.FormatUint(uint64(id), 10),
	})))
	if err != nil {
		return err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return errors.New(method + " error: status code not 200")
	}
	return nil
}
Exemple #3
0
// Thread-safe.
func (t *Trade) GetForeignInventory(contextId uint64, appId uint32, start *uint) (*inventory.PartialInventory, error) {
	data := map[string]string{
		"sessionid": t.sessionId,
		"steamid":   fmt.Sprintf("%d", t.other),
		"contextid": strconv.FormatUint(contextId, 10),
		"appid":     strconv.FormatUint(uint64(appId), 10),
	}
	if start != nil {
		data["start"] = strconv.FormatUint(uint64(*start), 10)
	}

	req, err := http.NewRequest("GET", t.baseUrl+"foreigninventory?"+netutil.ToUrlValues(data).Encode(), nil)
	if err != nil {
		panic(err)
	}
	req.Header.Add("Referer", t.baseUrl)

	return inventory.DoInventoryRequest(t.client, req)
}
Exemple #4
0
// Ajax POSTs to an API endpoint that should return a status
func (t *Trade) postWithStatus(url string, data map[string]string) (*Status, error) {
	status := new(Status)

	req := netutil.NewPostForm(url, netutil.ToUrlValues(data))
	// Tales of Madness and Pain, Episode 1: If you forget this, Steam will return an error
	// saying "missing required parameter", even though they are all there. IT WAS JUST THE HEADER, ARGH!
	req.Header.Add("Referer", t.baseUrl)

	resp, err := t.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	err = json.NewDecoder(resp.Body).Decode(status)
	if err != nil {
		return nil, err
	}
	return status, nil
}
Exemple #5
0
func (c *Client) getPartialTheirInventory(other steamid.SteamId, contextId uint64, appId uint32, start *uint) (*inventory.PartialInventory, error) {
	data := map[string]string{
		"sessionid": c.sessionId,
		"partner":   fmt.Sprintf("%d", other),
		"contextid": strconv.FormatUint(contextId, 10),
		"appid":     strconv.FormatUint(uint64(appId), 10),
	}
	if start != nil {
		data["start"] = strconv.FormatUint(uint64(*start), 10)
	}

	const baseUrl = "https://steamcommunity.com/tradeoffer/new/"
	req, err := http.NewRequest("GET", baseUrl+"partnerinventory/?"+netutil.ToUrlValues(data).Encode(), nil)
	if err != nil {
		panic(err)
	}
	req.Header.Add("Referer", baseUrl+"?partner="+fmt.Sprintf("%d", other))

	return inventory.DoInventoryRequest(c.client, req)
}
Exemple #6
0
// Sends a new trade offer to the given Steam user. You can optionally specify an access token if you've got one.
// In addition, `countered` can be non-nil, indicating the trade offer this is a counter for.
func (c *Client) Create(target steamid.SteamId, token string, myItems, theirItems []TradeItem, countered *TradeOfferId, message string) (*TradeCreateResult, error) {
	tradeoffer := map[string]interface{}{
		"newversion": true,
		"version":    3,
		"me": map[string]interface{}{
			"assets":   myItems,
			"currency": make([]struct{}, 0),
			"ready":    false,
		},
		"them": map[string]interface{}{
			"assets":   theirItems,
			"currency": make([]struct{}, 0),
			"ready":    false,
		},
	}

	jto, err := json.Marshal(tradeoffer)
	if err != nil {
		panic(err)
	}

	formFields := map[string]string{
		"sessionid":         c.sessionId,
		"serverid":          "1",
		"partner":           target.ToString(),
		"tradeoffermessage": message,
		"json_tradeoffer":   string(jto),
		"captcha":           "",
	}

	text, _ := json.Marshal(formFields)
	fmt.Println(string(text)) // Debug

	var referer string

	if countered != nil {
		referer = fmt.Sprintf("https://steamcommunity.com/tradeoffer/%d/", *countered)
		formFields["tradeofferid_countered"] = fmt.Sprintf("%d", *countered)
	} else {
		if token != "" {
			params := make(map[string]string)
			if token != "" {
				params["trade_offer_access_token"] = token
			}

			paramsJson, err := json.Marshal(params)
			if err != nil {
				panic(err)
			}

			formFields["trade_offer_create_params"] = string(paramsJson)

			referer = "https://steamcommunity.com/tradeoffer/new/?partner=" + strconv.FormatUint(uint64(target.GetAccountId()), 10) + "&token=" + token
		} else {
			referer = "https://steamcommunity.com/tradeoffer/new/?partner=" + strconv.FormatUint(uint64(target.GetAccountId()), 10)
		}
	}

	req := netutil.NewPostForm("https://steamcommunity.com/tradeoffer/new/send", netutil.ToUrlValues(formFields))
	req.Header.Add("Referer", referer)

	fmt.Println(req.Header) // Debug

	resp, err := c.client.Do(req)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	// If we failed, error out
	if resp.StatusCode != 200 {
		fmt.Println("StatusCode", resp.StatusCode) // Debug
		data, _ := ioutil.ReadAll(resp.Body)       // Debug
		log.Println("Refferer", referer)
		fmt.Println(string(data)) // Debug
		fmt.Println("From", formFields)

		return nil, errors.New("create error: status code not 200")
	}

	// Load the JSON result into TradeCreateResult
	result := new(TradeCreateResult)
	decoder := json.NewDecoder(resp.Body)
	decoder.Decode(result)
	return result, nil
}