【发布时间】:2025-12-07 23:55:02
【问题描述】:
我正在开发 Rails 4 应用程序,我正在使用/使用另一个站点的 API,例如 example.com,它使用 3-legged oauth 授权(与 twitter 相同)。为了实现这个功能,我使用了这个link 并实现了相同的功能。
这是我的实现
AuthController
class AuthController < ApplicationController
before_filter :authenticate_user!
before_filter :fetch_request_token, only: [:authorize]
def authorize
token = @consumer.get_request_token(oauth_callback: 'http://localhost:3000/auth/fetch_access_token')
authorize_url = token.authorize_url
redirect_to authorize_url
end
def fetch_access_token
acc_token = request_token.get_access_token(oauth_verifier: params[:oauth_verifier])
# remaining logic
redirect_to files_jds_path
end
private
def fetch_request_token
#logic for fetching request token
end
end
JDsController
class JDsController < AuthController
before_filter :authorize, only: [:files, :field_info] unless: :check_access_token
def files
# logic for the files
end
def field_info
# logic for the files
end
private
def check_access_token
# logic for checking access_token
end
end
目前我在任何操作之前检查 access_token 是否存在,如果 access_token 不存在,那么我正在使用授权方法获取 access_token。
如果您从AuthController(这是我的回调网址)中看到fetch_access_token 方法,我将重定向路径硬编码为files_jds_path。
由于这个实现虽然 before_filter 应用于field_info,但在获取 access_token 后它重定向到files_jds_path。
但我需要对其进行概括,以便对于任何操作,它都会重定向到相应的路径。
任何人都可以建议我该怎么做
【问题讨论】:
-
我想我明白你在问什么。您希望在获得授权后能够将某人送回他们正在寻找的原始页面吗?
-
我是否正确假设您使用的是基于令牌的身份验证而不是会话?
-
我正在使用基于会话的身份验证。当前的实现仅关联帐户。
标签: ruby-on-rails ruby redirect