使用Koala和Rails将视频发布到Facebook应用程序

时间:2012-08-10 13:46:03

标签: ruby-on-rails facebook facebook-graph-api oauth koala

我正在尝试将视频发布到用户使用Koala gem上传的Facebook应用墙。

当我尝试使用

发布视频时
put_connections("APP_ID", "APP_NAME:upload", video: "VIDEO_FILE")

我收到此错误:

OAuthException: (#240) Requires a valid user is specified (either via the session or via the API parameter for specifying the user.

以下是我的文件:

videos controller.rb

class VideosController < ApplicationController
    def index
        @videos = Video.all
    end

    def new 
        @video = Video.new
    end

    def show
        @video = Video.find params[:id]
    end

def upload
        @video = Video.create params[:video]
        if @video
             @upload_info = save_video_new_video_url(video_id: @video.id)
        else
             render "/videos/new"
        end
  end

def save_video
    @video = Video.find params[:id]
    if params[:id]
        Video.begin_upload current_user.id, params[:file]
    else
        Video.delete_video @video 
    end
  redirect_to videos_path, :notice => "video successfully uploaded"
end

end

video.rb

class Video < ActiveRecord::Base

    scope :completes,   where(:is_complete => true)
    scope :incompletes, where(:is_complete => false)


    def self.begin_upload user_id, video_file
        user = User.find user_id
        video = user.facebook.put_connections(FB_CONFIG['app_id'], "thecomfortmovement:upload", video: File.expand_path(video_file.tempfile.to_path.to_s))
    end


    def self.delete_incomplete_videos
        self.incompletes.map{|r| r.destroy}
    end

    def self.delete_video video
        vid = Video.find video
        vid.destroy
     end

end

我正在使用omniauth-facebook gem来验证用户,如此截屏视频中所示:http://railscasts.com/episodes/360-facebook-authentication

有谁知道我收到此错误的原因?我肯定仍然登录,这应该意味着会话和oauth令牌仍然有效。有什么建议吗?

更新

这是我的用户模型

class User < ActiveRecord::Base
    def self.from_omniauth(auth)
        where(auth.slice(:provider, :uid)).first_or_initialize.tap do |user|
        user.provider = auth.provider
        user.uid = auth.uid
        user.name = auth.info.name
        user.email = auth.info.email
        user.oauth_token = auth.credentials.token
        user.oauth_expires_at = Time.at(auth.credentials.expires_at)
        user.save!
     end
   end

   def facebook
        @facebook ||= Koala::Facebook::API.new(oauth_token)
   end

end

更新2

这是我用来设置和删除会话的控制器:

class SessionsController < ApplicationController

    def create
        user = User.from_omniauth(env["omniauth.auth"])
        session[:user_id] = user.id
        redirect_to root_url
    end

    def destroy
        session[:user_id] = nil
        redirect_to root_url
    end

end

0 个答案:

没有答案