Skip to content

Instantly share code, notes, and snippets.

@ikbear
Created May 29, 2014 04:03
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 ikbear/cbd81850414911c953f0 to your computer and use it in GitHub Desktop.
Save ikbear/cbd81850414911c953f0 to your computer and use it in GitHub Desktop.
生成七牛云存储的授权使用的 Access Token
require 'hmac-sha1'
require 'uri'
require 'base64'
def urlsafe_base64_encode(content)
Base64.encode64(content).strip.gsub('+', '-').gsub('/','_').gsub(/\r?\n/, '')
end
def generate_access_token(access_key, secret_key, url, body)
uri = URI.parse(url)
signature = uri.path
query_string = uri.query
signature += '?' + query_string if !query_string.nil? && !query_string.empty?
signature += "\n"
if body
signature += body
end
hmac = HMAC::SHA1.new(secret_key)
hmac.update(signature)
encoded_digest = urlsafe_base64_encode(hmac.digest)
token = %Q(#{access_key}:#{encoded_digest})
end
ak = 'LxxjLRHQoeKzZTNEbKllYdUFX3GbpoqKIuuy8zPe' # 换成正确的
sk = 'Yxxm5CDIEO7x1ZYarEHQ97fZYi4M4q9T8InM_zt' # 换成正确的
url = 'http://api.qiniu.com/stat/select/space?bucket=helishi&from=20140521&to=20140526&p=day'
token = 'QBox ' + generate_access_token(ak, sk, url, nil)
puts token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment