【问题标题】:Bad Request (400) when trying to create buy order on Steam尝试在 Steam 上创建购买订单时出现错误请求 (400)
【发布时间】:2020-04-24 18:40:15
【问题描述】:

我正在制作一个与 Steam 市场交互的 Python 机器人 (http://steamcommunity.com/market)。一切顺利,但是我坚持创建购买订单。我的 Python(3) 代码基于以下 javascript:

$J.ajax( {
    url: 'https://steamcommunity.com/market/createbuyorder/',
    type: 'POST',
    data: {
        sessionid: g_sessionID,
        currency: g_rgWalletInfo['wallet_currency'],
        appid: this.m_unAppId, // ITEM?
        market_hash_name: this.m_strMarketHashName,
        price_total: price_total,
        quantity: quantity
    },
    crossDomain: true,
    xhrFields: { withCredentials: true }
} ).done( function ( data ) {
    CreateBuyOrderDialog.OnCreateBuyOrderComplete( { responseJSON: data } );
} ).fail( function( jqxhr ) {
    // jquery doesn't parse json on fail
    var data = $J.parseJSON( jqxhr.responseText );
    CreateBuyOrderDialog.OnCreateBuyOrderComplete( { responseJSON: data } );
} );

其中currency/appid/price_total/quantity 是整数,sessionid 和market_hash_name 是字符串。我已将此代码重构为 Python 3:

cj = http.cookiejar.CookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)

#before the request is made, cookies are 'created' by doing other requests on the same website

def placeOrder():

    url = 'http://steamcommunity.com/market/createbuyorder'
    values = {'sessionid' : self.sessionid,
              'currency' : '3',
              'appid' : '730',
              'market_hash_name' : 'Chroma 2 Case',
              'price_total' : '4',
              'quantity' : '1'}
    headers = {'Accept' : '*/*',
               'Content-type' : 'application/x-www-form-urlencoded; charset=UTF-8',
               'Referer' : 'http://steamcommunity.com/market/listings/730/Chroma%202%20Case',
               'Accept-Language' : 'nl-NL',
               'Origin' : 'http://steamcommunity.com',
               'Accept-Encoding' : 'gzip, deflate',
               'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko',
               'Host' : 'steamcommunity.com',
               'Connection' : 'Keep-Alive',
               'Cache-Control' : 'no-cache'}
    post = urllib.parse.urlencode(values)
    binary_data = post.encode('utf-8')
    request = urllib.request.Request(url, binary_data, headers)
    response = urllib.request.urlopen(request)

    data = json.loads(response.decode('utf-8'))

但是在调用placeOrder() 函数时会返回此错误:urllib.error.HTTPError: HTTP Error 400: Bad Request

为什么这会产生错误的请求?因为我通过购买商品在此 url http://steamcommunity.com/market/listings/730/Chroma%202%20Case# 上创建了一个“真实”请求,从而准确地复制了标头和 cookie。

为什么网站会返回错误的请求?最有可能的是什么? (例如,缺少 cookie、缺少标头、缺少步骤等)

感谢您的帮助!

【问题讨论】:

  • 我也遇到了同样的问题。通过在浏览器上创建一个真实的请求,我已经准确地复制了标题和 cookie。你是怎么解决这个错误的

标签: javascript python http cookies request


【解决方案1】:

urlhttp://steamcommunity.com/market/createbuyorder 在您的 Python 代码中缺少尾随 /,这是一个区别。

这个很难调试,发送的数据是一样的吗?

【讨论】:

    【解决方案2】:

    也许为时已晚,但我得到了一些建议。 Ajax 请求中的 url 是“https”而不是“http”在这里可能很重要,错误请求的另一个可能原因可能是凭据,尝试使用标头: "Access-Control-Allow-Origin": "http://steamcommunity.com""Access-Control-Allow-Credentials": "true"。请告诉我您是否解决了问题以及如何解决。

    【讨论】:

    • 是的,我确实解决了,但我不知道怎么解决了。如果你有机会也制作一个机器人,请告诉我:> - 我让我的完全工作
    • 是的,我也在尝试使用机器人。我昨天解决了错误请求的问题,现在开始让它正常工作。
    • 我得到了一个完全可以处理持久会话的东西。我买了一个 VPS,因为我与家里的主机连接不好但是由于某种原因,脚本返回了错误的请求(这主要意味着有人已经通过列表购买购买了它)但是它在我的 VPS 上不起作用......如果你想要我可以把我的脚本发给你(如果你愿意,因为自己编写代码会更好)而且它只是一个带有listingids的项目的脚本,而不是buyorder的。在我制作真正的蒸汽机器人之前,我制作了这个可能对你有用:github.com/wagenaartje/steamBot/blob/master/steamBot.py
    • 我真正的蒸汽机器人实际上是一个脚本,而且速度要快得多......我已经从中获利,但不是超级骗子(如果你在一台性能良好的机器上运行它,可能每天最多 50 美分) ping)...根据我的不良 ping,如果您整天运行它,每天最多 20 美分。如果您希望我发送代码,请告诉我...
    • 我对您如何在市场上扫描低价商品的部分感兴趣。如果您在登录时发送请求,则需要更长的时间。是的,很高兴看到您的代码。
    猜你喜欢
    • 2019-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-21
    相关资源
    最近更新 更多