Quickblox Rails 签名

Quickblox Rails Signature

我很难访问 quickblox API。根据他们的文档,这段代码应该可以工作:

 require 'base64'
  require 'cgi'
  require 'openssl'
  require 'hmac-sha1'

  # Application credentials
  aPPLICATION_ID = 12345
  aUTH_KEY = 'hidden'
  aUTH_SECRET = 'hidden'


  # Generate signature
  timestamp = Time.now.in_time_zone('UTC').to_i
  nonce = timestamp-425346
  signature_string = "application_id=#{aPPLICATION_ID}&auth_key=#{aUTH_KEY}&nonce=#{nonce}&timestamp=#{timestamp}"
  signature =Base64.encode64("#{ OpenSSL::HMAC.digest('sha1', signature_string, aUTH_SECRET) }")

  # Post
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
  request = Net::HTTP::Post.new("/session.json")
  request.add_field('QuickBlox-REST-API-Version', '0.1.1')
  request.add_field('Content-Type', 'application/json')
  request.add_field('Accept', '*/*')
  request.body = {"application_id" => aPPLICATION_ID, "auth_key" => aUTH_KEY, "nonce" => nonce, "timestamp" => timestamp, "signature" => signature }.to_json

  response = http.request(request)

但是我一直收到错误消息:{"errors":{"base":["Unexpected signature"]}}

即使使用他们的 hurl: http://hurl.quickblox.com/ 我也会得到完全相同的错误。非常令人沮丧。我到底做错了什么?

事实证明,QuickBlox 似乎出于某种原因不接受 ruby 生成的签名。 QuickBlox 甚至删除了他们自己的 ruby gem(其中也会产生同样的错误)。

现在的解决方案是先通过 php 脚本获取令牌,然后在 ruby 中调用。

在此处勾选

这主要归结为这两行:

signature_string = "application_id=#{aPPLICATION_ID}&auth_key=#{aUTH_KEY}&nonce=#{nonce}&timestamp=#{timestamp}"

signature =Base64.encode64("#{ OpenSSL::HMAC.digest('sha1', signature_string, aUTH_SECRET) }")

你也可以检查这个quickblox_api gem。它对我很有用。