2011-01-24 99 views
2

ķ家伙,所以我是用dialybooth API workign和OAauth,所以首先,我不得不直线上升不能存储在会话的OAuth令牌(导轨)

#first redirect the user to the authorize_url 
    redirect_to dailybooth.authorize_url 

#on user return grab the code from the query string 
dailybooth.oauth_token(params[:code]) 

#make request to the api 
pp dailybooth.get('/users.json') 

问题是,它会不断的重定向,因为它甚至没有检查oauth_token是否被设置,所以我做了这个;现在

unless dailybooth.oauth_token(params[:code]) 
#first redirect the user to the authorize_url 
redirect_to dailybooth.authorize_url 

#on user return grab the code from the query string 
dailybooth.oauth_token(params[:code]) 
end 


#make request to the api 
pp dailybooth.get('/users.json') 

,这个送我去dailybooth授权页,授权用户,然后发送到我的网页,我获得了他们的帐户(与返回的用户信息的数组)访问,事情是,如果您刷新的令牌不再存在,并且用户必须重新授权。

于是,我就什么是对的oauth_token存储在会话,

if session[:oauth_code] #If session is set 
    dailybooth.oauth_token(session[:oauth_code]) #sign in using cookie 
else 
    if params[:code] 
    @oauth_token_ = params[:code] 
    session[:oauth_code] = @oauth_token_ 
    else 
     #first redirect the user to the authorize_url 
     redirect_to dailybooth.authorize_url 
    end 
end 



#make request to the api 
@info = dailybooth.get('/users.json') 

if @info['error']  
    if @info['error']['error_code'] 
    if @info['error']['error_code'] == 302 #if getting invalid token, request another token. 
     session[:oauth_code] = nil 
     #first redirect the user to the authorize_url 
     redirect_to dailybooth.authorize_url 

    end 
    end 
end 

我仍然得到同样的事情,当我第一次去我的网站,我重定向到授权页面,当局,然后我可以访问该帐户,但是当我尝试返回索引时,它说oauth_token是无效的。任何帮助?

回答

0

我会说实话,我不是100%确定的(并且DailyBooth的API文档是......缺乏,至少可以这么说),但我相信你使用“token”(你是从params[:code]获得并存储在@oauth_token_中)以获得访问令牌,然后存储访问令牌(例如,如果是对象/数组,则将结果存储为dailybooth.oauth_token(session[:oauth_code])或至少其一部分)。

unless session[:oauth_code] #unless the session is set 
    if params[:code] 
    session[:oauth_code] = dailybooth.oauth_token(params[:code]) 
    else 
    #first redirect the user to the authorize_url 
    redirect_to dailybooth.authorize_url 
    end 
end 

#make request to the api 
@info = dailybooth.get('/users.json') 

if @info['error']  
    if @info['error']['error_code'] 
    if @info['error']['error_code'] == 302 #if getting invalid token, request another token. 
     session[:oauth_code] = nil 
     #first redirect the user to the authorize_url 
     redirect_to dailybooth.authorize_url 

    end 
    end 
end 

我希望这点能指向正确的方向。