Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

twitter oauth で荒川智則になるやつ基本の部分

  • Loading branch information...
commit 499eae95ffb5ddb0f33084d5ecd5022f45177d8b 1 parent de1c2d4
@yuiseki authored
View
1  .gitignore
@@ -1,2 +1,3 @@
public/back/summer/**/*
gdocs.csv
+settings.yaml
View
13 app.rb
@@ -8,6 +8,9 @@
require "helpers/gdocs"
configure do
+ #require 'openssl'
+ #OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
+ use Rack::Session::Cookie, :secret => Digest::SHA1.hexdigest(rand.to_s)
set :settings, YAML.load_file("settings.yaml")
set :account, Pit.get("google.com", :require => {
"username" => "your email in Google",
@@ -15,8 +18,12 @@
})
end
-get '/' do
+def sitemap(path, desc)
+end
+
+get'/', {'sitemap' => ['/', 'サイトのトップページです']} do
@title = options.settings["site"]["title"]
+ @desc = "サイトのトップページです"
@result = get_recents
haml :root
end
@@ -27,7 +34,7 @@
end
get '/rss' do
- create_rss
+ create_rss
end
get '/entry/:time' do
@@ -38,6 +45,8 @@
get 'page/' do
end
+load "extras/oauth.rb"
+
# ランダム背景画像
load "extras/back.rb"
# Dropbox共有フォルダ公開
View
67 extras/oauth.rb
@@ -0,0 +1,67 @@
+require 'oauth'
+require 'twitter'
+# twitter oauth
+# gemsがバグっているので以下を参照して修復する必要あり
+# http://d.hatena.ne.jp/hypercrab/20100704/1278182883
+# oauth, twitter どちらも
+def base_url
+ default_port = (request.scheme == "http") ? 80 : 443
+ port = (request.port == default_port) ? "" : ":#{request.port.to_s}"
+ "#{request.scheme}://#{request.host}#{port}"
+end
+
+def oauth_consumer
+ OAuth::Consumer.new(
+ options.settings["oauth"]["key"],
+ options.settings["oauth"]["secret"],
+ :site => "http://twitter.com")
+end
+
+get '/twitter/auth' do
+ # redirect to twitter oauth
+ puts "starting oauth"
+ puts options.settings["oauth"].inspect
+ callback_url = "#{base_url}/twitter/return"
+ puts callback_url
+ begin
+ request_token = oauth_consumer.get_request_token(:oauth_callback => callback_url)
+ rescue OAuth::Unauthorized => @exception
+ return erb %{ error: <%= @exception %> : <%= Time.now %> }
+ end
+ session[:request_token] = request_token.token
+ session[:request_token_secret] = request_token.secret
+ redirect request_token.authorize_url
+end
+
+get '/twitter/return' do
+ puts "returned oauth"
+ # return from twitter auth confirmed
+ request_token = OAuth::RequestToken.new(
+ oauth_consumer, session[:request_token], session[:request_token_secret])
+ begin
+ @access_token = request_token.get_access_token(
+ {},
+ :oauth_token => params[:oauth_token],
+ :oauth_verifier => params[:oauth_verifier])
+ rescue OAuth::Unauthorized => @exception
+ return erb %{ error: <%= @exception %> }
+ end
+ session[:access_token] = @access_token.token
+ session[:access_token_secret] = @access_token.secret
+
+ twitter_oauth = Twitter::OAuth.new(
+ options.settings["oauth"]["key"],
+ options.settings["oauth"]["secret"]
+ )
+ twitter_oauth.authorize_from_access(session[:access_token], session[:access_token_secret])
+ twitter = Twitter::Base.new(twitter_oauth)
+ twitter.update_profile({:name=>"荒川智則"})
+ screen_name = twitter.update_profile["screen_name"]
+ log = {:screen_name => screen_name,
+ :access_token => session[:access_token],
+ :access_token_secret => session[:access_token_secret],
+ :twitter_oauth => true}
+ puts log.inspect
+ redirect "http://twitter.com/"+screen_name
+end
+
View
6 settings.yaml → settings.default.yaml
@@ -3,7 +3,11 @@ app:
backend:
type: "google_spreadsheet"
- sheet_key: "tZ034W7OBe2_nmdh5XVw8Tg"
+ sheet_key: your_spreadsheet_key
+
+oauth:
+ key: your_oauth_key
+ secret: your_oauth_secret
site:
url: "http://xn--fdr45z90g374a.jp/"
View
10 views/layout.haml
@@ -7,12 +7,4 @@
%body
%div{:class=>"content"}
%h1{:class=>"title"}=@title
- - @result.each do |i|
- - unless i[1] == ""
- %div{:class=>"article"}
- %a{:href=>i[3], :style=>"text-decoration: none;"}
- %h2= i[1]
- %p{:style=>"color:black;"}= i[0]
- %pre{:style=>"color:black;"}= i[2]
- - unless i[4] == ""
- %img{:src=>i[4], :style=>"max-height:500px; max-width:500px;"}
+ = yield
View
9 views/root.haml
@@ -0,0 +1,9 @@
+- @result.each do |i|
+ - unless i[1] == ""
+ %div{:class=>"article"}
+ %a{:href=>i[3], :style=>"text-decoration: none;"}
+ %h2= i[1]
+ %p{:style=>"color:black;"}= i[0]
+ %pre{:style=>"color:black;"}= i[2]
+ - unless i[4] == ""
+ %img{:src=>i[4], :style=>"max-height:500px; max-width:500px;"}
View
8 views/twitter_auth.haml
@@ -0,0 +1,8 @@
+%div{:class=>"article"}
+ %h2=@subtitle
+ %ul
+ %li="名前を荒川智則に変更します"
+ %li="tokenは保存せず、作業後破棄します"
+ %form{:action=>"", :method=>"POST"}
+ %input{:type=>"submit", :value=>"了解"}
+
Please sign in to comment.
Something went wrong with that request. Please try again.