Welcome to FutureAppLaboratory

v=(*^ワ^*)=v

Upload Pics by Imgur Api in Ruby

| Comments

Introduction

Sample ruby script. Use Imgur’s api to upload image, with OAuth2 authorization.

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env ruby
# encoding: utf-8

require 'httpclient'
require 'json'

class ImgurUploader

  AUTH_URL = 'https://api.imgur.com/oauth2/authorize'
  EXC_TOKEN_URL = 'https://api.imgur.com/oauth2/token'
  UPLOAD_URL = 'https://api.imgur.com/3/image'
  CLIENT_ID = 'YOUR_CLIENT_ID'
  CLIENT_SE = 'YOUR_CLIENT_SECRET_KEY'

  @file_path = ""
  @access_token = ""

  def initialize(file_path)
    @file_path = file_path
  end

  def setAccessToken(access_token)
    @access_token = access_token
  end

  def applyAccessToken
    params = {
      :client_id => CLIENT_ID,
      :response_type => "pin",
      :state => 'init'
    }.map{ |x,v| "#{x}=#{v}" }.reduce{|x,v| "#{x}&#{v}" }

    system('open', AUTH_URL + "?" + params)

    puts "input PIN here: "
    mypin = STDIN.gets.chomp

    body = {
      :client_id => CLIENT_ID,
      :client_secret => CLIENT_SE,
      :grant_type => "pin",
      :pin => mypin
    }.map{ |x,v| "#{x}=#{v}" }.reduce{|x,v| "#{x}&#{v}" }

    client = HTTPClient.new
    @res = client.post(EXC_TOKEN_URL, body)

    puts @res.body

    result = JSON.load(@res.body)

    @access_token = result['access_token']
    puts "access_token: " + @access_token

    File.open("access_token.tmp", 'w') { # store access_token to local file,
      # no need to authorize again if access_token is not expired
      |f| f.write @access_token
    }
  end

  def upload
    auth_header = { 'Authorization' => 'Bearer ' + @access_token }
    client = HTTPClient.new
    File.open(@file_path) do |file|
      body = { 'image' => file }
      @res = client.post(UPLOAD_URL, body, auth_header)
      end
    begin
      result = JSON.load(@res.body)
      puts @res.body
      result['status']
    rescue => ex
      puts ex.message
      puts "upload failed on Authorization"
      -1
    end
  end

end

if ARGV.length < 1
  puts "usage"
  puts "ruby imgur.rb [image file path]"
  exit
end

uploader = ImgurUploader.new(ARGV[0].chomp.strip)

begin
  File.open("access_token.tmp", 'r') { # read local access_token
    |f| uploader.setAccessToken(f.gets)
  }
  if uploader.upload == 200 # if uploaded
    puts "Upload ok"
  else # if upload failed by expired token
    puts "Upload failed, try to update access_token"
    uploader.applyAccessToken
    if uploader.upload == 200 # upload again
      puts "Upload ok"
    else # still fails
      puts "Upload failed, need detail check"
    end
  end
rescue => ex # local file not exists, apply for access_token and upload
  puts ex.message
  puts "No access_token is stored. Do Authorization again."
  uploader.applyAccessToken
  uploader.upload
end

Comments