【发布时间】: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