【问题标题】:Cannot store oauth token in session (rails)无法在会话中存储 oauth 令牌(rails)
【发布时间】:2011-06-14 06:58:57
【问题描述】:

K 伙计们,所以我正在使用 dialybooth api 和 OAauth,所以首先,我直接了

 #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 无效。有什么帮助吗?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 oauth


    【解决方案1】:

    说实话,我不是 100% 确定(而且 DailyBooth 的 API 文档……至少可以说是缺乏),但我相信您使用“令牌”(您得到的那个)从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
    

    我希望这会为您指明正确的方向。

    【讨论】:

      猜你喜欢
      • 2018-02-05
      • 1970-01-01
      • 2021-01-14
      • 2012-01-18
      • 2021-07-02
      • 2011-09-12
      • 2018-05-06
      • 2019-08-08
      • 1970-01-01
      相关资源
      最近更新 更多