- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 mechanize 发送 Steam 交易报价,我进行了登录以获取所需的 cookie,但是当我尝试发送 Steam 交易报价时,我收到错误 401 Unauthorized。
我从 python 移植了 this code 唯一的区别是,据我所知,与 ruby 的 Mechanize 相比,python 的请求库如何处理 POST 请求中的 cookie,您可以验证我在登录请求中获取了所有 cookie通过输出 Mechanize cookie 并根据 this 我拥有所有必要的 cookie
这是我的代码,您可以复制粘贴它并执行它,唯一的问题是最后几行。
require 'mechanize'
require 'json'
require 'open-uri'
require 'openssl'
require 'base64'
require 'time'
def fa(shared_secret)
timestamp = Time.new.to_i
math = timestamp / 30
math = math.to_i
time_buffer =[math].pack('Q>')
hmac = OpenSSL::HMAC.digest('sha1', Base64.decode64(shared_secret), time_buffer)
start = hmac[19].ord & 0xf
last = start + 4
pre = hmac[start..last]
fullcode = pre.unpack('I>')[0] & 0x7fffffff
chars = '23456789BCDFGHJKMNPQRTVWXY'
code= ''
for looper in 0..4 do
copy = fullcode #divmod
i = copy % chars.length #divmod
fullcode = copy / chars.length #divmod
code = code + chars[i]
end
puts code
return code
end
def pass_stamp(username,password,mech)
response = mech.post('https://store.steampowered.com/login/getrsakey/', {'username' => username})
data = JSON::parse(response.body)
mod = data["publickey_mod"].hex
exp = data["publickey_exp"].hex
timestamp = data["timestamp"]
key = OpenSSL::PKey::RSA.new
key.e = OpenSSL::BN.new(exp)
key.n = OpenSSL::BN.new(mod)
ep = Base64.encode64(key.public_encrypt(password.force_encoding("utf-8"))).gsub("\n", '')
return {'password' => ep, 'timestamp' => timestamp }
end
user = 'user'
password = 'password'
session = Mechanize.new { |agent|
agent.user_agent_alias = 'Windows Mozilla'
agent.follow_meta_refresh = true
agent.add_auth('https://steamcommunity.com/tradeoffer/new/send/', user, password)
agent.log = Logger.new("mech.log")
}
data = pass_stamp(user,password, session)
ep = data["password"]
timestamp = data["timestamp"]
session.add_auth('https://steamcommunity.com/tradeoffer/new/send/', user, ep)
send = {
'password' => ep,
'username' => user,
'twofactorcode' =>fa('twofactorcode'), #update
'emailauth' => '',
'loginfriendlyname' => '',
'captchagid' => '-1',
'captcha_text' => '',
'emailsteamid' => '',
'rsatimestamp' => timestamp,
'remember_login' => 'false'
}
login = session.post('https://store.steampowered.com/login/dologin', send )
responsejson = JSON::parse(login.body)
if responsejson["success"] != true
puts "didn't sucded"
puts "probably 2fa code time diffrence, retry "
exit
end
responsejson["transfer_urls"].each { |url|
getcookies = session.post(url, responsejson["transfer_parameters"])
}
session.get("https://steamcommunity.com/") do |page| ## to verify that you are logged in check this HTML
File.open('./body.html', 'w') {|f| f.puts page.content}
end
sessionid = ''
session.cookies.each { |c|
string = c.dup.to_s
if string.include?('sessionid')
sessionid = string.gsub('sessionid=', '')
end
}
offer_link = 'https://steamcommunity.com/tradeoffer/new/?partner=410155236&token=H-yK-GFt'
token = offer_link.split('token=', 2)[1]
theirs = [{"appid" => 753,"contextid"=> "6","assetid" => "6705710171","amount" => 1 }]
mine = []
params = {
'sessionid' => sessionid,
'serverid' => 1,
'partner' => '76561198370420964',
'tradeoffermessage' => '',
'json_tradeoffer' => {
"new_version" => true,
"version" => 4,
"me" => {
"assets" => mine, #create this array
"currency" => [],
"ready" => false
},
"them" => {
"assets" => theirs, #create this array
"currency" => [],
"ready" => false
}
},
'captcha' => '',
'trade_offer_create_params' => {'trade_offer_access_token' => token}
}
#the issue begins from here
begin
send_offer = session.post(
'http://steamcommunity.com/tradeoffer/new/send/',
params,
{'Referer' => "#{offer_link}", 'Origin' => 'https://steamcommunity.com/tradeoffer/new/send' }
)
puts send_offer.body
rescue Mechanize::UnauthorizedError => e
puts e
puts e.page.content
end
最佳答案
我通过调试 python POST 请求发现了这个问题。
发生了什么:当我登录时,我确实得到了 sessionid,但是 sessionid 对“store.steampowered.com”和“help.steampowered.com”有效,恰好是“.storesteapowered.com”。
在我的代码中,我盲目地识别了我的 session cookie(没有注意它属于哪个网站),因此在 POST 请求参数中发送的 sessionid 变量不等于 POST 请求正在发送的 cookie标题所以我得到了 401 Unauthorized。
所以我们需要为 steamcommunity.com 设置/获取 session ID。
修复:
1)为 steamcommunity.com 设置一个随机的 CSRF sessionid cookie,或者像我一样,将 steampowered.com 的 session id cookie 设置为 steamcommunity.com(在代码中标记)
2) 在 params => 'json_tradeoffer' => "new_version"
中应该是 "newversion"
以避免错误 400 BAD REQUEST
3)post请求的头部应该是:
{'Referer' =>'https://steamcommunity.com/tradeoffer/new', 'Origin' =>'https://steamcommunity.com' }
4) 使用 to_json 将
params => json_tradeoffer
和
params => 'trade_offer_create_params'
值转换为字符串
require 'mechanize'
require 'json'
require 'open-uri'
require 'openssl'
require 'base64'
require 'time'
def fa(shared_secret)
timestamp = Time.new.to_i
math = timestamp / 30
math = math.to_i
time_buffer =[math].pack('Q>')
hmac = OpenSSL::HMAC.digest('sha1', Base64.decode64(shared_secret), time_buffer)
start = hmac[19].ord & 0xf
last = start + 4
pre = hmac[start..last]
fullcode = pre.unpack('I>')[0] & 0x7fffffff
chars = '23456789BCDFGHJKMNPQRTVWXY'
code= ''
for looper in 0..4 do
copy = fullcode #divmod
i = copy % chars.length #divmod
fullcode = copy / chars.length #divmod
code = code + chars[i]
end
puts code
return code
end
def pass_stamp(username,password,mech)
response = mech.post('https://store.steampowered.com/login/getrsakey/', {'username' => username})
data = JSON::parse(response.body)
mod = data["publickey_mod"].hex
exp = data["publickey_exp"].hex
timestamp = data["timestamp"]
key = OpenSSL::PKey::RSA.new
key.e = OpenSSL::BN.new(exp)
key.n = OpenSSL::BN.new(mod)
ep = Base64.encode64(key.public_encrypt(password.force_encoding("utf-8"))).gsub("\n", '')
return {'password' => ep, 'timestamp' => timestamp }
end
user = 'user'
password = 'password'
session = Mechanize.new { |agent|
agent.user_agent_alias = 'Windows Mozilla'
agent.follow_meta_refresh = true
agent.add_auth('https://steamcommunity.com/tradeoffer/new/send/', user, password)
agent.log = Logger.new("mech.log")
}
data = pass_stamp(user,password, session)
ep = data["password"]
timestamp = data["timestamp"]
session.add_auth('https://steamcommunity.com/tradeoffer/new/send/', user, ep)
send = {
'password' => ep,
'username' => user,
'twofactorcode' =>fa('twofactorcode'), #update
'emailauth' => '',
'loginfriendlyname' => '',
'captchagid' => '-1',
'captcha_text' => '',
'emailsteamid' => '',
'rsatimestamp' => timestamp,
'remember_login' => 'false'
}
login = session.post('https://store.steampowered.com/login/dologin', send )
responsejson = JSON::parse(login.body)
if responsejson["success"] != true
puts "didn't sucded"
puts "probably 2fa code time diffrence, retry "
exit
end
responsejson["transfer_urls"].each { |url|
getcookies = session.post(url, responsejson["transfer_parameters"])
}
## SET COOKIE FOR STEAM COMMUNITY.COM
steampowered_sessionid = ''
session.cookies.each { |c|
if c.name == "sessionid"
steampowered_sessionid = c.value
puts c.domain
end
}
cookie = Mechanize::Cookie.new :domain => 'steamcommunity.com', :name =>'sessionid', :value =>steampowered_sessionid, :path => '/'
session.cookie_jar << cookie
sessionid = steampowered_sessionid
### END SET COOKIE
offer_link = 'https://steamcommunity.com/tradeoffer/new/?partner=410155236&token=H-yK-GFt'
token = offer_link.split('token=', 2)[1]
theirs = [{"appid" => 753,"contextid"=> "6","assetid" => "6705710171","amount" => 1 }]
mine = []
params = {
'sessionid' => sessionid,
'serverid' => 1,
'partner' => '76561198370420964',
'tradeoffermessage' => '',
'json_tradeoffer' => {
"newversion" => true, ## FIXED newversion to avoid 400 BAD REQUEST
"version" => 4,
"me" => {
"assets" => mine, #create this array
"currency" => [],
"ready" => false
},
"them" => {
"assets" => theirs, #create this array
"currency" => [],
"ready" => false
}
}.to_json, # ADDED TO JSON TO AVOID 400 BAD REQUEST
'captcha' => '',
'trade_offer_create_params' => {'trade_offer_access_token' => token}.to_json ## ADDED TO JSON FIX TO AVOID ERROR 400 BAD REQUEST
}
begin
send_offer = session.post(
'https://steamcommunity.com/tradeoffer/new/send',
params,
{'Referer' => 'https://steamcommunity.com/tradeoffer/new', 'Origin' => 'https://steamcommunity.com' } ##FIXED THIS
)
puts send_offer.body
rescue Mechanize::UnauthorizedError => e
puts e
puts e.page.content
end
关于发送 POST 请求时 Ruby 的 Mechanize 错误 401(Steam 交易报价发送),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49928648/
我听过很多次公司喜欢HBase的强一致性。我阅读了 HBase 并喜欢它。然后我想到了mongodb write和那时候的区别。查了一下MongoDB似乎也有很强的一致性。但它是一致的吗?看起来 HB
区块链入门 ③ - 交易 交易 概述 比特币交易本质上包含交易参与者价值转移的相关信息数据结构。比特币区块链是一本全球复式记账总账簿,每笔交易都是在比特币区块链上的一个公开记录.
我有以下情况: 我正在迭代我的Affiliate 实体,对于每个实体,我需要在一个唯一的事务中保存和更新数据。因此,我有一个服务,其方法用 Spring @Transactional 注释(其中创建和
我无法理解 DaoManager 的默认行为。 DaoManager.createDao(connectionSource, theClass); 这需要一个 connectionSource - 而
我是 Spring 新手,有一个关于事务的问题。 我知道对于每个 http 请求都有一个 servlet 线程,它有自己的堆栈。据我所知,所有局部变量和方法都驻留在堆栈上。因此,如果我有一个方法 pu
我想设计一个简单的应用程序(没有 j2ee 和 jms),可以处理大量消息(比如在交易系统中) 我创建了一个服务,可以接收消息并将它们放入队列中,这样系统就不会在过载时卡住。 然后我创建了一个包装队列
如果使用 PDO 事务,是否需要锁定表? 如果用户 a 有 50 笔钱,将 50 笔转给用户 b,PDO 交易是否会确保它们都无误地执行? 另外,如果说我有一个 if 语句, if ($user['m
我正在实现一个方法,它会做类似的事情: ... try { myPojo.setProperty("foo"); myService.execute(myPojo); } catch (E
我正在尝试使用 ActiveRecord::Base.transaction。我认为使用 Rails 1.2.6 和 mysql 5.0 默认情况下回滚不起作用。多玩一点我发现 autocommit
我在我的网站上使用嵌入式支付,支付交易直接从买家到卖家发起,服务充当 API 调用方。商品价格由卖家以美元设定,以简化国际贸易。 当发件人和收件人都是俄罗斯居民时,发件人会收到错误消息: The pa
如果我删除我的应用程序中的数据,然后重新购买一些我知道该帐户已经拥有的托管 IAP,iOS 会给我原生的“您确定要重新购买该项目吗?您不会被收取费用”对话框。这符合预期。 当购买返回到我的应用程序时,
我一直在阅读 transactions & jooq但我很难看到如何在实践中实现它。 假设我为 JOOQ 提供了一个自定义 ConnectionProvider,它恰好使用了一个自动提交设置为 fal
我们正在使用 Entity Framework 并在事务范围内运行单元测试。我们最初在标题中遇到错误。 我已经设法将问题隔离开来。 using (TransactionScope scope1 = n
我有一个注册页面,基本上我需要将数据插入到 4 个表中。我是 PDO 的新手,对某些事情感到困惑。 基本上,如果任何插入失败,我不想向数据库中添加任何内容,这看起来很简单。 我的困惑是,我需要首先在我
我使用枢轴点进行交易。我在屏幕指示器上使用以下指标“CM_Pivots_Filtered”、“Pivots”、“CD_PivotR”和“CM_Gaps_Intra-Day_V2.1”。这些枢轴工作得很
我正在努力解决 Sonar 问题: squid:S2229 "Methods should not call same-class methods with incompatible "@Transa
在我的 Controller 中,我有一些类似的代码... ... if user.save something = Something.where("thing = ?", thing)
我使用 StoreKit 进行应用内购买。我发现当用户按下“取消”按钮时,API 的行为很奇怪。 例如,如果我在“确认您的应用内购买”屏幕上按“取消”,我会收到一个带有 error.code == S
AppStore 在自动续订自动续订订阅时是否会发出交易?如果是这样,如果应用程序将自己设置为观察者,那么下次应用程序加载时是否可以可靠地检测到它: [[SKPaymentQueue defaultQ
我正在研究 EMV 技术,并寻找终端和发行者之间的通信(请求/响应)以进行授权/在线 PIN 检查。 我知道离线数据验证仅在终端上进行检查,然后终端将数据发送给发行者。我想知道授权过程需要发送哪些数据
我是一名优秀的程序员,十分优秀!