app/controllers/users_controller.rb
# User authorized us at Google site
User authorized us at Google site
User authorized us at Google site
def authsub
    request_token = OAuth::RequestToken.new(get_consumer, params[:oauth_token],session[:oauth_secret])
    access_token = request_token.get_access_token(
                      {:oauth_verifier => params[:oauth_verifier]})
    body = access_token.get("http://gdata.youtube.com/feeds/api/users/default").body
    oauth_user = Nokogiri::XML.parse(body).search("author").children.first.text
    user = User.find_by_youtube_username(oauth_user)
    if user
      if is_logged_in? && !is_me?(user)
        flash[:error] = "抱歉，這個Youtube帳戶 #{user.youtube_username} 已經連結至其他使用者了！"
        redirect_back_or_default('/')
      else
        user.update_attributes(
          :oauth_token => access_token.token, 
          :oauth_secret => access_token.secret)
        session[:oauth_token] = access_token.token
        session[:oauth_secret] = access_token.secret
        self.logged_in_user = user
        self.logged_in_user.increment!(:signin_count)
        flash[:notice] = "嗨 #{oauth_user}，你已經透過Youtube帳戶登入"
        return redirect_to('/')
      end
    else
      if is_logged_in?
        logged_in_user.update_attributes(
          :oauth_token => access_token.token, 
          :oauth_secret => access_token.secret, 
          :youtube_username => oauth_user)
        flash[:notice] = "已驗證你的Youtube帳號 #{oauth_user}，下次你可以使用這個帳號登入"
        return redirect_to user_path(logged_in_user)
      else
        @user = User.new({
          :oauth_token => access_token.token, 
          :oauth_secret => access_token.secret, 
          :youtube_username => oauth_user})
        flash[:notice] = "嗨 #{oauth_user}，你已完成Google帳戶驗證，接下來我們需要您填寫一些基本資料"
        render :action => 'new'
      end
    end
  end
def authsub request_token = OAuth :: RequestToken . new ( get_consumer , params [ :oauth_token ] , session [ :oauth_secret ] ) access_token = request_token . get_access_token ( { :oauth_verifier => params [ :oauth_verifier ] } ) body = access_token . get ( "http://gdata.youtube.com/feeds/api/users/default" ) . body oauth_user = Nokogiri :: XML . parse ( body ) . search ( "author" ) . children . first . text user = User . find_by_youtube_username ( oauth_user ) if user if is_logged_in? && ! is_me? ( user ) flash [ :error ] = "抱歉，這個Youtube帳戶 #{user.youtube_username} 已經連結至其他使用者了！" redirect_back_or_default ( '/' ) else user . update_attributes ( :oauth_token => access_token . token , :oauth_secret => access_token . secret ) session [ :oauth_token ] = access_token . token session [ :oauth_secret ] = access_token . secret self . logged_in_user = user self . logged_in_user . increment! ( :signin_count ) flash [ :notice ] = "嗨 #{oauth_user}，你已經透過Youtube帳戶登入" return redirect_to ( '/' ) end else if is_logged_in? logged_in_user . update_attributes ( :oauth_token => access_token . token , :oauth_secret => access_token . secret , :youtube_username => oauth_user ) flash [ :notice ] = "已驗證你的Youtube帳號 #{oauth_user}，下次你可以使用這個帳號登入" return redirect_to user_path ( logged_in_user ) else @user = User . new ( { :oauth_token => access_token . token , :oauth_secret => access_token . secret , :youtube_username => oauth_user } ) flash [ :notice ] = "嗨 #{oauth_user}，你已完成Google帳戶驗證，接下來我們需要您填寫一些基本資料" render :action => 'new' end end end
User authorized us at Google site
User authorized us at Google site
# Recreate the (now authorized) request token # Swap the authorized request token for an access token                                        