云短信

  • Ruby SDK

    最近更新时间:2019-07-26 17:04:40

    鉴权和请求客户端封装

    # coding: utf-8
    require 'openssl'
    require 'base64'
    require 'uri'
    require 'net/http'
    require 'net/https'
    require 'json'
    
    module SMS
      class Credentials
    
        attr_reader :access_key, :secret_key
    
        def initialize(access_key, secret_key)
          @access_key = access_key
          @secret_key = secret_key
        end
    
    
        class << self
    
          def base64_url_encode(string)
            Base64.encode64(string).tr("+/", "-_").gsub(/[\n\r]?/, "")
          end
    
          def digest(secret, bytes)
            OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha1'), secret, bytes)
          end
    
          def sign(secret, bytes)
            base64_url_encode(digest(secret, bytes))
          end
    
          def generate_signature(options = {})
            method        = options[:method]
            url           = options[:url]
            content_type  = options[:content_type]
            body          = options[:body]
    
            uri = URI.parse(url)
    
            signature = "#{method} #{uri.path}"
    
            query_string = uri.query
    
            if !query_string.nil? && !query_string.empty?
              signature += '?' + query_string
            end
    
            signature += "\nHost: #{uri.host}"
    
            if !content_type.nil? && !content_type.empty? && content_type != "application/octet-stream"
              signature += "\nContent-Type: #{content_type}"
            end
    
            signature += "\n\n"
    
            if body.is_a?(Hash)
              signature += body.to_json
            end
    
            return signature
          end
    
        end
      end
    
      class RPC
        API_BASE_URL = "https://sms.qiniuapi.com/v1"
    
        class << self
    
          def post(credentials, url, body)
            url = API_BASE_URL + url
    
            signature_options = {
              :url => url,
              :content_type => "application/json",
              :method => "POST",
              :body => body
            }
    
            encoded_sign = Credentials.sign(credentials.secret_key, Credentials.generate_signature(signature_options))
    
            uri = URI.parse(url)
            https = Net::HTTP.new(uri.host,uri.port)
            https.use_ssl = true
            req = Net::HTTP::Post.new(uri.path)
    
            req["Authorization"] = "Qiniu #{credentials.access_key}:#{encoded_sign}"
            req["Content-Type"] = "application/json"
    
            req.body = body.to_json
    
            res = https.request(req)
          end
        end
      end
    end
    

    调用示例

    mac = SMS::Credentials.new("access_key", "secret_key")
    
    body = {
      :template_id => "template_id",
      :mobile => "mobile",
      :parameters => {
        :code => "code"
      }
    }
    
    SMS::RPC.post(mac, "/message/single", body)
    
    以上内容是否对您有帮助?
  • Icon free helper
    Close