所以我终于结束了使用dropbox-sdk编写所有内容
控制器
class DropboxController < ApplicationController
def new
db_session = DropboxSession.new(MULTIAPI_CONFIG['dropbox']['appKey'], MULTIAPI_CONFIG['dropbox']['appSecret'])
begin
db_session.get_request_token
rescue DropboxError => e
render template: "multi_api/home/refresh"
end
session[:dp_request_db_session] = db_session.serialize
# OAuth Step 2: Send the user to the Dropbox website so they can authorize
# our app. After the user authorizes our app, Dropbox will redirect them
# to our 'dp_callback' endpoint.
auth_url = db_session.get_authorize_url url_for(:dp_callback)
redirect_to auth_url
end
def destroy
session.delete(:dp_authorized_db_session)
render :json => checkAuth
end
def isAuthenticated
render :json => checkAuth
end
def checkAuth
val = {'isAuthenticated' => false}
begin
unless not session[:dp_authorized_db_session]
dbsession = DropboxSession.deserialize(session[:dp_authorized_db_session])
client = DropboxClient.new(dbsession, MULTIAPI_CONFIG['dropbox']['accessType'])
val = {'isAuthenticated' => true}
end
rescue DropboxError => e
val = {'isAuthenticated' => false}
end
val
end
def callback
# Finish OAuth Step 2
ser = session[:dp_request_db_session]
unless ser
render template: "multi_api/home/refresh"
return
end
db_session = DropboxSession.deserialize(ser)
# OAuth Step 3: Get an access token from Dropbox.
begin
db_session.get_access_token
rescue DropboxError => e
render template: "multi_api/home/refresh"
return
end
session.delete(:dp_request_db_session)
session[:dp_authorized_db_session] = db_session.serialize
render template: "multi_api/home/refresh"
end
end
路线
get 'dp/logout', :to => 'dropbox#destroy'
get 'dp/login', :to => 'dropbox#new'
get 'dp/callback', :to => 'dropbox#callback', :as => :dp_callback
get 'dp/isAuthenticated', :to => 'dropbox#isAuthenticated'
multi_api/home/refresh.html.erb
<script type="text/javascript">
function refreshParent()
{
window.opener.location.reload();
if (window.opener.progressWindow)
window.opener.progressWindow.close();
window.close();
}
refreshParent();
</script>
请求保管箱
dbsession = DropboxSession.deserialize(session[:dp_authorized_db_session])
@client = DropboxClient.new(dbsession, MULTIAPI_CONFIG['dropbox']['accessType'])
当我想向 Dropbox 验证用户身份时,我会打开一个新选项卡,完成后我会自动刷新原始页面并关闭选项卡(请参阅:)multi_pi/home/refresh.html.erb
。
由于我在 javascript 中完成所有这些操作,因此我需要知道用户是否已成功通过身份验证,这就是为什么我提供了一个路由,该路由dp/isAuthenticated
将返回一个包含'isAuthenticated'
设置为true
or的密钥的 json 字符串false
。
连接的用户不会保存到数据库中,只会保存到会话中。因此,当会话被销毁时,他们将不得不重新进行身份验证。如果您希望将它们保存到数据库中,那么您应该深入研究@smarx 解决方案,使用omniauth 会容易得多。
我在这里写了我的代码作为那些只想依赖ruby dropbox-sdk 的示例