Uploading an image to imgur.com using ruby
- Go to http://www.imgur.com and create an account.
- Go to https://api.imgur.com/oauth2/addclient
- Give a name to your app
- Select Anonymous usage without user authorization in Authorization type.
- Provide your email.
- Give a description for the app.
- Fill in the captcha.
- Click Submit.
- You will get an email with your client_id and your client_secret.
- If you intend to upload anonymously, you only need to use your client_id.
- Make a POST request to https://api.imgur.com/3/upload.json with the following properties:
- A header called
Authorization
with the valueClient-ID <client_id>
. - A parameter called
image
with the image data encoded in Base64.
- A header called
Example in ruby (replace <client_id>
with your client_id ):
#!/usr/bin/ruby
require 'net/http'
require 'uri'
require 'base64'
require 'json'
filename = "image.png"
imagedata = Base64.encode64(File.read(filename))
url = "https://api.imgur.com/3/upload.json"
params = {image: imagedata}
uri = URI.parse(url)
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
request = Net::HTTP::Post.new(uri.path)
request["Authorization"] = "Client-ID <client_id>"
request.set_form_data(params)
response = https.request(request)
hash = JSON.parse(response.body)
link = hash["data"]["link"]
puts hash
puts link