diff --git a/item.go b/item.go index 9b8915d..5d33d3a 100644 --- a/item.go +++ b/item.go @@ -8,6 +8,7 @@ import ( "net/http" "net/url" "strconv" + "strings" ) // Item struct containing data on retrieved items @@ -40,25 +41,43 @@ func (s *Session) HasItem(id int) (bool, error) { } // AddItem add item by id to inventory -func (s *Session) AddItem(id int, seller int) error { - v := url.Values{} - v.Set("rqtype", "purchase") - v.Set("productID", strconv.Itoa(id)) - v.Set("expectedCurrency", "1") - v.Set("expectedPrice", "0") - v.Set("expectedSellerID", strconv.Itoa(seller)) - v.Set("userAssetID", "") - resp, err := s.client.Post("https://www.roblox.com/api/item.ashx?"+v.Encode(), "application/json", bytes.NewBufferString("")) +func (s *Session) AddItem(id int) error { + product, err := s.GetProduct(id) if err != nil { return err } + payload := struct { + ExpectedCurrency int `json:"expectedCurrency"` + ExpectedPrice int `json:"expectedPrice"` + ExpectedSellerID int `json:"expectedSellerId"` + }{ + ExpectedCurrency: 1, + ExpectedPrice: 0, + ExpectedSellerID: product.Creator.ID, + } + // Gen json body + data, err := json.Marshal(&payload) + if err != nil { + return err + } + + req, err := http.NewRequest("POST", fmt.Sprintf("https://economy.roblox.com/v1/purchases/products/%d", product.ProductID), bytes.NewBuffer(data)) + if err != nil { + return err + } + req.Header.Set("Content-Type", "application/json") + + resp, err := s.client.Do(req) + if err != nil { + return err + } defer resp.Body.Close() if resp.StatusCode == 403 { - req, err := http.NewRequest("POST", "https://www.roblox.com/api/item.ashx?"+v.Encode(), bytes.NewBufferString("")) - req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0]) + req, err := http.NewRequest("POST", fmt.Sprintf("https://economy.roblox.com/v1/purchases/products/%d", product.ProductID), bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0]) resp, err := s.client.Do(req) if err != nil { @@ -87,7 +106,7 @@ func (s *Session) RemoveItem(id int) error { if resp.StatusCode == 403 { req, err := http.NewRequest("POST", "https://www.roblox.com/asset/delete-from-inventory", bytes.NewBufferString(v.Encode())) - req.Header.Set("X-Csrf-Token", resp.Header["X-Csrf-Token"][0]) + req.Header.Set("x-csrf-token", strings.Trim(resp.Header["X-Csrf-Token"][0], " ")) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") resp, err := s.client.Do(req) diff --git a/session.go b/session.go index 2106614..b32979a 100644 --- a/session.go +++ b/session.go @@ -24,6 +24,7 @@ func New(cookie string) (*Session, error) { // url.Parse("http://www.roblox.com") // http://api.roblox.com cookieJar.SetCookies(&url.URL{Scheme: "http", Host: "www.roblox.com"}, rbxCookie) cookieJar.SetCookies(&url.URL{Scheme: "http", Host: "api.roblox.com"}, rbxCookie) + cookieJar.SetCookies(&url.URL{Scheme: "https", Host: "economy.roblox.com"}, rbxCookie) client := &http.Client{ Jar: cookieJar, }