1

ここのチュートリアルと同様にアプリをセットアップします-http://railscasts.com/episodes/235-devise-and-omniauth-revised。アクセスできない場合は、以下が私のコードです

Omniauthコントローラーのコールバック

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

ユーザーモデル

def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.username = auth.info.nickname
      user.name = auth.info.name

    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

認証されたユーザーのoauthトークンとoauthトークンシークレットを取得するにはどうすればよいですか?

ありがとう

4

1 に答える 1

3

特定のプロバイダーから返される情報を確認したい場合は、これをコールバックコントローラーの最初の行として配置します。

raise env["omniauth.auth"].to_yaml

auth.credentials.tokenとで必要な情報にアクセスできることがわかりますauth.credentials.secret

編集:Rails4がbetter_errorsgemを使用するようになったため、omniauthハッシュを検査するこの方法はあまりうまく機能しなくなりました。今のより良い方法は次のとおりです。

render :text => "<pre>" + env["omniauth.auth"].to_yaml and return
于 2013-04-01T13:24:35.460 に答える