Rails - 设计 - Twitter 和 Instagram 没有重定向到我的回调 url

Rails - Devise - Twitter and Instagram are not redirecting to my callback url

我正在使用 Devise 和 Omniauth 通过社交媒体进行用户身份验证。我在 config/initializers/devise.rb 中添加了以下内容:

config.omniauth :facebook, ENV['FACEBOOK_APP_ID'], ENV['FACEBOOK_APP_SECRET'], scope: 'public_profile,email', callback_url: "#{ENV['SERVER_ROOT']}/users/auth/facebook/callback"
config.omniauth :google_oauth2, ENV['GOOGLE_APP_ID'], ENV['GOOGLE_APP_SECRET'], scope: 'userinfo.email,userinfo.profile', redirect_uri: "#{ENV['SERVER_ROOT']}/users/auth/google_oauth2/callback"
config.omniauth :twitter, ENV['TWITTER_APP_ID'], ENV['TWITTER_APP_SECRET'], callback_url: "#{ENV['SERVER_ROOT']}/users/auth/twitter/callback"
config.omniauth :instagram, ENV['INSTAGRAM_APP_ID'], ENV['INSTAGRAM_APP_SECRET'], callback_url: "#{ENV['SERVER_ROOT']}/users/auth/instagram/callback"

在我的 app/controllers/omniauth_callbacks_controller.rb 中:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController

    def all
        user = User.from_omniauth(request.env["omniauth.auth"])
        if user.persisted?
          sign_in_and_redirect user, notice: "Signed in!"
        else
          session["devise.user_attributes"] = user.attributes
          redirect_to new_user_session_url
        end
      end
      alias_method :twitter, :all
      alias_method :google_oauth2, :all
      alias_method :facebook, :all
      alias_method :instagram, :all
    end
end

app/models/users.rb我补充说:

class User < ApplicationRecord

    devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable

    def self.from_omniauth(auth)
        where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
            user.provider = auth.provider
            user.email = auth.info.email || if auth.info.nickname then auth.info.nickname + "@twitter.org" end
            user.uid = auth.uid
            user.skip_confirmation!
        end
    end

    def self.new_with_session(params, session)
        if session["devise.user_attributes"]
            new(session["devise.user_attributes"], without_protection: true) do user
                user.attributes = params
                user.valid?
            end
        else
            super
        end
    end

    def password_required?
        super && provider.blank?
    end
end

我还配置了社交媒体应用程序(或客户端)以重定向到 https://<my domain name>/users/auth/<social media>/callback

现在,我可以使用 Facebook 和 Google 登录。但是 Twitter 重定向到:

https://api.twitter.com/users/auth/twitter/callback?oauth_token=<some token> with message "Sorry, that page doesn’t exist!" (not the callback url).

并且 Instagram 重定向到:

https://www.instagram.com/oauth/authorize?client_id=<client id>&redirect_uri=/users/auth/instagram/callback&response_type=code&scope=basic&state=<some state number>

JSON 响应:

{"error_type": "OAuthException", "code": 400, "error_message": "Redirect URI does not match registered redirect URI"}

注意:回调 url 我在 Twitter 应用程序中输入的是 https://<my domain>/users/auth/twitter/callback

我在我的 Instagram 客户端中输入的重定向 uri 是 https://<my domain>/users/auth/instagram/callback

我将 config/initializers/devise.rb 中的 Instagram 行更改为:

config.omniauth :instagram, ENV['INSTAGRAM_APP_ID'], ENV['INSTAGRAM_APP_SECRET'], redirect_uri: "#{ENV['SERVER_ROOT']}/users/auth/instagram/callback"

但是,身份验证也会重定向到:

https://www.instagram.com/oauth/authorize?client_id=<client id>&redirect_uri=/users/auth/instagram/callback&response_type=code&scope=basic&state=<some state number>

从 Nginx 中删除 proxy_set_header X-Forwarded-Proto: $scheme; 后有效。显然,这是应用程序和 Nginx 之间的 https 连接。