1

OmniAuth Facebook ログイン フローをサーバー側の Rails アプリケーションに統合することに成功しました。ただし、クライアント側で Facebook Javascript SDK を使用してこれを機能させようとしており、いくつかの問題が発生しています。

編集: この問題は Chrome でのみ発生しているようで、Safari や Firefox では発生していないようです

セッション コントローラー- これはサーバー側のフローで機能します

def create

      auth = request.env['omniauth.auth']
      #if an authorization does not exisit, it will create a new authorization record. it will also create a new user record if a user is not currently logged in
      unless @auth = Authorization.find_from_hash(auth)
        # Create a new user or add an auth to existing user, depending on
        # whether there is already a user signed in.
        @auth = Authorization.create_from_hash(auth, current_user)

        #add the friends array to user record. as of now only doing this on the initial user create
        @friends = []
        FbGraph::User.me(@auth.user.authorization.facebook_token).fetch.friends.each do |t|
          @friends << t.identifier
        end
        u = @auth.user
        u.facebook_friends = @friends
        u.save

      end

      #store a new auth token if needed (if the new token in the hash does not match the one stored in the database for authorization)
      Authorization.check_if_new_auth_token_is_needed(auth)

      # Log the authorizing user in.
      self.current_user = @auth.user

      redirect_to root_url

  end

/auth/facebook パスを押すだけで、ユーザーはログインします

ルート

match '/auth/:provider/callback', :to => 'sessions#create'

ホームページビューで、クライアント側フローログインを実行しようとしています

ホームページビュー

<script>
$(function() {
  $('a').click(function(e) {
    e.preventDefault();
    FB.login(function(response) {
      if (response.authResponse) {
        $('#connect').html('Connected! Hitting OmniAuth callback (GET /auth/facebook/callback)...');
        // since we have cookies enabled, this request will allow omniauth to parse
        // out the auth code from the signed request in the fbsr_XXX cookie
        $.getJSON('/auth/facebook/callback', function(json) {
          $('#connect').html('Connected! Callback complete.');
          $('#results').html(JSON.stringify(json));
        });
      }
    }, { scope: 'email,publish_stream' }); 
  });
});
</script>
<p id="connect">
          <a href="#">Connect to FB</a>
        </p>

        <p id="results" />

ログに次のエラーが表示されます

{"エラー":{"メッセージ":"認証コードがありません","タイプ":"OAuthException","コード":1}}

基本的に、Omniauth は FB.login アクションからの facebook 署名付きリクエストを取得していません ( https://github.com/mkdynamic/omniauth-facebook/blob/master/example/config.ruが想定しているように)。

これを適切に機能させる方法や、間違っている可能性があることについてのアイデアはありますか?

4

1 に答える 1