Login to your Flickr account.
Then create an app in the Flickr App Garden by going to the following link:

https://www.flickr.com/services/apps/create/

All the apps in the App Garden are created by Flickr members using the Flickr API. Here’s how:

– Get your API Key
Ready to build something? You’ll need a key first. Request an API Key by going to the following link:

https://www.flickr.com/services/apps/create/apply/

Then, Apply for a Non-Commercial Key
And enter the name of your app and What are you building and agree to the terms then submit the details.

After creating your new app you will get the API Key and Secret for your new app, save the credentials with you.

 

Now, create a new rails app:
$ rails new flickr-demo

Go to the rails app directory:
$ cd flickr-demo

 

Open Gemfile and add required gems:
gem ‘flickraw’
gem ‘figaro’

 

Run bundle command to install these gems:
$ bundle install

Now, install figaro:
$ figaro install

This creates a config/application.yml file and adds it to your .gitignore.

 

Open config/application.yml file and add following lines:
API_KEY:
SHARED_SECRET:

ACCESS_TOKEN:
ACCESS_SECRET:

 

Now, paste your API key and secret you got while creating your app in the flickr App Garden:
API_KEY: cb0b3bc291c43c4e6011962dsf34ghy6
SHARED_SECRET: 6dae31d5bhgtr45f6g

 

Create the database:
$ rake db:create

 

Now, go to rails console for getting access token and access secret:
$ rails console

> require ‘flickraw’
> FlickRaw.api_key=”cb0b3bc291c43c4e6011962dsf34ghy6″
> FlickRaw.shared_secret=”6dae31d5bhgtr45f6g”

> token = flickr.get_request_token
> auth_url = flickr.get_authorize_url(token[‘oauth_token’], :perms => ‘delete’)

Now, Open this auth_url in your browser to complete the authentication process.
Copy, the number given when you complete the process.

> verify = <the unique number given when you completed the process>

> begin
>     flickr.get_access_token(token[‘oauth_token’], token[‘oauth_token_secret’], verify)
>     login = flickr.test.login
>     puts “You are now authenticated as #{login.username} with token #{flickr.access_token} and secret #{flickr.access_secret}”
> rescue FlickRaw::FailedResponse => e
>     puts “Authentication failed : #{e.msg}”
> end

 

After this you will get your access token and access secret, now paste them in config/application.yml file:
ACCESS_TOKEN: 72157645091675344-343e8fd409454098
ACCESS_SECRET: 898f78253176845s

 

Now, your config/application.yml file should contain four variables:
API_KEY: cb0b3bc291c43c4e6011962dsf34ghy6
SHARED_SECRET: 6dae31d5bhgtr45f6g

ACCESS_TOKEN: 72157645091675344-343e8fd409454098
ACCESS_SECRET: 898f78253176845s

 

Now, close your rails console.

And, create a new model photo:
$ rails generate model photo photo_id:string photo_url:string

 

Now, create a new controller photos:
$ rails generate controller photos

 

Now, paste below code in controller app/controllers/photos_controller.rb

class PhotosController < ApplicationController
require ‘flickraw’

before_action :set_photo, only: [:show, :destroy]
before_action :set_flickr, only: [:create, :destroy]

def index
@photos = Photo.all
end

def show
end

def new
@photo = Photo.new
end

def create
photo_id = flickr.upload_photo params[:photo].tempfile.path, :title => “Title”, :description => “Description”
photo_url = FlickRaw.url_o(flickr.photos.getInfo(photo_id: photo_id))

@photo = Photo.new(photo_id: photo_id, photo_url: photo_url)

respond_to do |format|
if @photo.save
format.html { redirect_to @photo, notice: ‘Photo was successfully created.’ }
format.json { render action: ‘show’, status: :created, location: @photo }
else
format.html { render action: ‘new’ }
format.json { render json: @photo.errors, status: :unprocessable_entity }
end
end
end

def destroy
flickr.photos.delete(photo_id: @photo.photo_id)
@photo.destroy
respond_to do |format|
format.html { redirect_to photos_url }
format.json { head :no_content }
end
end

private
def set_photo
@photo = Photo.find(params[:id])
end

def set_flickr
FlickRaw.api_key = ENV[‘API_KEY’]
FlickRaw.shared_secret = ENV[‘SHARED_SECRET’]

flickr.access_token = ENV[‘ACCESS_TOKEN’]
flickr.access_secret = ENV[‘ACCESS_SECRET’]
end
end

 

Now, open config/routes.rb file and paste below lines:
resources :photos, only: [:index, :show, :new, :create, :destroy]
root ‘photos#index’

 

Go to app/views/photos folder and create 4 files named as:
index.html.erb, show.html.erb, new.html.erb, _form.html.erb

 

Open, index.html.erb file and paste below code:

<h1>Listing photos</h1>

<table>
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>

<tbody>
<% @photos.each do |photo| %>
<tr>
<td><img src=”<%= photo.photo_url %>” style=”width: 150px;height: 100px;”></td>
<td><%= link_to ‘Show’, photo %></td>
<td><%= link_to ‘Destroy’, photo, method: :delete, data: { confirm: ‘Are you sure?’ } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>

<%= link_to ‘New Photo’, new_photo_path %>

 

Open, show.html.erb file and paste below code:

<p id=”notice”><%= notice %></p>

<img src=”<%= @photo.photo_url %>”>
<br/>
<%= link_to ‘Back’, photos_path %>

 

Open, new.html.erb file and paste below code:

<h1>New photo</h1>

<%= render ‘form’ %>

<%= link_to ‘Back’, photos_path %>

 

Open, _form.html.erb file and paste below code:

<%= form_for(@photo, html: {:multipart => true}) do |f| %>
<% if @photo.errors.any? %>
<div id=”error_explanation”>
<h2><%= pluralize(@photo.errors.count, “error”) %> prohibited this photo from being saved:</h2>

<ul>
<% @photo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<%= file_field_tag :photo %>
<%= f.submit %>
<% end %>
Now, after doing all the required changes run following commands to get the app running:
$ rake db:migrate
$ rails server

Open http://localhost:3000 in any browser to run your flickr-demo app and enjoy saving your images on flickr.
Flickr URL Helpers:

There are some helpers to build flickr urls :
url, url_m, url_s, url_t, url_b, url_z, url_q, url_n, url_c, url_o, url_profile, url_photopage, url_photoset, url_photosets, url_short, url_short_m, url_short_s, url_short_t, url_photostream

You will find all Flickraw Photos Api methods here:
http://hanklords.github.io/flickraw/FlickRaw/Flickr/Photos.html

 

Save images to flickr using flickraw gem

2 thoughts on “Save images to flickr using flickraw gem

  • March 10, 2015 at 7:33 am
    Permalink

    Thank you very much for this excellent example! It helped me a lot. But i also need to be able to render photos from flickr by query (f.e. “cats”). Trying to make it work 🙂 Very good tutorial for begginers.

    Cheers,
    Andrei.

    Reply
  • August 12, 2015 at 7:12 pm
    Permalink

    Thanks for the great tutorial. However (since I’m a Rails Newbie), everything that happens inside the #create and #destroy actions (in the PhotosController) seems to be magic for me. Although your blog post is rather old, could you explain what is going on there?

    Reply

Leave a Reply

Your email address will not be published.