|
require "sinatra/base" |
|
require "flickraw" |
|
require "twitpic" |
|
require "oauth" |
|
|
|
FLICKR = { |
|
:api_key => "...", |
|
:secret_key => "..." |
|
} |
|
TWITTER = { |
|
:api_key => "...", |
|
:secret_key => "...", |
|
:twitpic_api_key => "..." |
|
} |
|
|
|
class Share < Sinatra::Base |
|
def initialize |
|
FlickRaw.secure = true |
|
FlickRaw.api_key = FLICKR[:api_key] |
|
FlickRaw.shared_secret = FLICKR[:secret_key] |
|
|
|
@picture = "../temp/pic.jpg" |
|
|
|
super |
|
end |
|
|
|
configure do |
|
use Rack::Session::Pool |
|
set :time_at_startup, Time.now |
|
end |
|
|
|
before do |
|
session[:flickr] ||= FlickRaw::Flickr.new() |
|
session[:twitter] ||= OAuth::Consumer.new( |
|
TWITTER[:api_key], |
|
TWITTER[:secret_key], |
|
:site => "https://api.twitter.com", |
|
:request_token_path => "/oauth/request_token", |
|
:authorize_path => "/oauth/authorize", |
|
:access_token_path => "/oauth/access_token" |
|
) |
|
if session[:twitpic].nil? |
|
session[:twitpic] = TwitPic::Client.new() |
|
session[:twitpic].configure do |conf| |
|
conf.api_key = TWITTER[:twitpic_api_key] |
|
conf.consumer_key = TWITTER[:api_key] |
|
conf.consumer_secret = TWITTER[:secret_key] |
|
end |
|
end |
|
end |
|
|
|
get "/twitter/connect.json" do |
|
begin |
|
session[:twitpic].upload_and_post(@picture, "test title").to_yaml |
|
|
|
"{response: \"updated\"}" |
|
rescue Nestful::ForbiddenAccess |
|
"{response: \"the same\"}" |
|
rescue Nestful::UnauthorizedAccess |
|
twitter_token = session[:twitter].get_request_token :oauth_callback => to("/twitter/callback.json") |
|
session[:twitter_token] = twitter_token |
|
redirect twitter_token.authorize_url |
|
end |
|
end |
|
|
|
get "/twitter/callback.json" do |
|
twitter_token = session[:twitter_token] |
|
twitter_access_token = twitter_token.get_access_token :oauth_verifier => params["oauth_verifier"] |
|
session[:twitpic].config.oauth_token = twitter_access_token.token |
|
session[:twitpic].config.oauth_secret = twitter_access_token.secret |
|
|
|
begin |
|
session[:twitpic].upload_and_post @picture, "test title" |
|
"{response: \"updated\"}" |
|
rescue Nestful::ForbiddenAccess |
|
"{response: \"the same\"}" |
|
end |
|
end |
|
|
|
get "/flickr/connect.json" do |
|
begin |
|
session[:flickr].upload_photo @picture, :title => "test title" |
|
"{response: \"updated\"}" |
|
rescue FlickRaw::OAuthClient::FailedResponse |
|
flickr_token = session[:flickr].get_request_token :oauth_callback => to("/flickr/callback.json") |
|
session[:flickr_token] = flickr_token |
|
redirect session[:flickr].get_authorize_url(flickr_token["oauth_token"], :perms => "write") |
|
end |
|
end |
|
|
|
get "/flickr/callback.json" do |
|
flickr_token = session[:flickr_token] |
|
session[:flickr].get_access_token flickr_token["oauth_token"], flickr_token["oauth_token_secret"], params["oauth_verifier"] |
|
session[:flickr].upload_photo @picture, :title => "test title" |
|
|
|
"{response: \"updated\"}" |
|
end |
|
end |