Skip to content

Instantly share code, notes, and snippets.

@flanker
Created August 23, 2018 02:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flanker/36aae5dd7c8d01d8670fa0165944510d to your computer and use it in GitHub Desktop.
Save flanker/36aae5dd7c8d01d8670fa0165944510d to your computer and use it in GitHub Desktop.
tencent cloud service - invoice ocr api
require 'net/http'
require 'openssl'
require 'base64'
require 'json'
appid = 'APP_ID'
secret_id = 'SECRET_ID'
secret_key = 'SECRET_KEY'
current_time = Time.now.to_i
expired_time = current_time + 24 * 60 * 60
once = (rand * 10000000).to_i
api_url = 'https://recognition.image.myqcloud.com/ocr/invoice'
image_url = 'THE_INVOICE_IMAGE_URL'
sign = "a=#{appid}&b=&k=#{secret_id}&e=#{expired_time}&t=#{current_time}&r=#{once}&f="
encrypted_sign = OpenSSL::HMAC.digest('sha1', secret_key, sign)
authorization = Base64.strict_encode64("#{encrypted_sign}#{sign}")
uri = URI(api_url)
Net::HTTP.start(uri.host, uri.port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
request = Net::HTTP::Post.new uri.request_uri
request.add_field('Content-Type', 'application/json')
request.add_field('Authorization', authorization)
request.body = {
appid: appid,
url: image_url
}.to_json
response = http.request request
puts response
puts response.body
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment