Created
September 1, 2012 10:39
-
-
Save fadhlirahim/3569270 to your computer and use it in GitHub Desktop.
Sinatra app Authenticating with Viki API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'bundler' | |
Bundler.require(:default) | |
enable :sessions | |
# Register your application at http://www.viki.com/applications | |
# to get the application_id and application_secret | |
# The callback url is /auth/test/callback | |
def client | |
OAuth2::Client.new("application_id", "application_secret", :site => "http://viki.com/oauth/token") | |
end | |
get "/auth/test" do | |
redirect client.auth_code.authorize_url(:redirect_uri => redirect_uri) | |
end | |
get '/auth/test/callback' do | |
access_token = client.auth_code.get_token(params[:code], :redirect_uri => redirect_uri) | |
session[:access_token] = access_token.token | |
@message = "Successfully authenticated with the server" | |
erb :success | |
end | |
get '/movies' do | |
@movies = get_response('movies.json') | |
erb :movies | |
end | |
def get_response(url) | |
access_token = OAuth2::AccessToken.new(client, session[:access_token]) | |
JSON.parse(access_token.get("/api/v3/#{url}").body) | |
end | |
def redirect_uri | |
uri = URI.parse(request.url) | |
uri.path = '/auth/test/callback' | |
uri.query = nil | |
uri.to_s | |
end | |
get "/" do | |
'Start with /auth/test' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "./app" | |
run Sinatra::Application |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source :rubygems | |
gem 'sinatra' | |
gem 'oauth2' | |
gem 'json' | |
group :test do | |
gem 'rspec' | |
gem 'rack-test' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= @movies.inspect %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require File.join(File.dirname(__FILE__), '../app') | |
require 'rubygems' | |
require 'bundler' | |
Bundler.setup(:default, :test) | |
require 'rack/test' | |
set :environment, :test | |
set :run, false | |
set :raise_erros, true | |
set :logging, false | |
def app | |
Sinatra::Application | |
end | |
RSpec.configure do |config| | |
config.include(Rack::Test::Methods) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Put this file inside views folder | |
Success authentication! | |
<%= @message.inspect %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment