2

我正在使用 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 应用中输入的回调 url 是https://<my domain>/users/auth/twitter/callback

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

我将 Instagram 线路更改config/initializers/devise.rb为:

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>
4

1 回答 1

0

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

于 2018-05-06T14:56:12.290 回答