我正在尝试将用户使用 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.
这是我的文件:
视频控制器.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
视频.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