如果存在移动应用程序而不是网络表单,Twitter OAuth 应该打开移动应用程序进行授权

Twitter OAuth should open mobile app for authorization if the mobile app is present instead of the web form

在我的 rails 网络项目中,用户通过 Twitter 进行身份验证。 oauth 过程涉及用户,通过 Twitter 网络表单输入 Twitter 登录凭据,这很有效。我想知道如果用户安装了 Twitter 的移动应用程序,用户是否可以通过它授权我的 Web 项目。

所以基本上,当用户访问我的 Web 项目并单击“使用 Twitter 登录”时,如果用户安装了 Twitter 的移动应用程序,则授权过程应该发生,否则它使用 Web 表单。我只在 periscope 移动应用程序中看到了这种可能性,其中对 Twitter 的授权发生在 Twitter 的移动应用程序中。因为我的项目是基于网络的,所以这甚至可能吗?有趣的是,我在我的网络项目中有一个 link 到我的推特个人资料,当我点击它时 link 打开我的移动推特应用程序。

这是我的 rails 应用程序中的当前 oauth 进程。这是来自我的用户模型的相关代码

 def self.from_omniauth(auth)
    user = find_or_initialize_by(provider: auth.provider, uid: auth.uid)
    user.email = auth.info.email
    user.password = Devise.friendly_token[0, 20]
    user.name = auth.info.name
    user.username = auth.info.nickname
    user.location = auth.info.location
    user.access_token = auth.credentials.token
    user.access_secret = auth.credentials.secret
    user.access_token = user.encrypt_field(user.access_token)
    user.access_secret = user.encrypt_field(user.access_secret)
    user.save!
    return user
  end

在我的用户控制器中,我有

 def twitter
    @user = User.from_omniauth(request.env["omniauth.auth"])
    if @user.persisted?
      sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
      set_flash_message(:notice, :success, kind: "Twitter") if is_navigational_format?
    else
      session["devise.twitter_data"] = request.env["omniauth.auth"].except("extra")
      redirect_to new_user_registration_url
    end
  end

在我的 gemfile 中,我有

gem 'devise'
gem 'omniauth'
gem 'omniauth-twitter'
gem 'twitter'

这适用于基于 Web 表单的身份验证。如果用户安装了 Twitter 的 phone 应用程序,我想知道如何或是否可以通过它进行身份验证和授权。

对于基于 Web 的流程,OAuth 流程将发生在 Twitter 网站上,而不是应用程序上。使用现已弃用的 TwitterKit 的原生应用支持原生移动流程。