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

时间:2018-04-26 13:08:29

标签: ruby-on-rails devise instagram omniauth omniauth-twitter

我正在使用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"}

注意:我在Twitter应用中输入的回拨网址为https://<my domain>/users/auth/twitter/callback

并重定向uri我输入我的Instagram客户端是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>

1 个答案:

答案 0 :(得分:0)

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