title: Rails + OmniAuth Facebook: how to obtain Access Token?
body: <p>I am trying to fetch the list of friends from Facebook. Sign in through Facebook is not a problem, but the problem is to fetch person's friends - because of <code>access token</code>.</p>
  
  <pre><code>puts request.env["omniauth.auth"].inspect
  puts '==='
  @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
  @fb_user = FbGraph::User.fetch(@user.uid).friends
  puts @fb_user.inspect
  </code></pre>
  
  <p>The problem is on the #4 line - in this case I am getting error</p>
  
  <pre><code>OAuthException :: An access token is required to request this resource.
  </code></pre>
  
  <p>When I put there something like this:</p>
  
  <pre><code>@fb_user = FbGraph::User.fetch(request.env["omniauth.auth"].credentials.token).friends
  </code></pre>
  
  <p>I'll get</p>
  
  <pre><code>OAuthException :: (#803) Some of the aliases you requested do not exist: PRINTED OUT TOKEN
  </code></pre>
  
  <p>What's the proper way to obtain the <code>access token</code>?</p>
  
  <h2><strong>EDIT: Current flow</strong></h2>
  
  <pre><code>class Users::OmniauthCallbacksController &lt; Devise::OmniauthCallbacksController
    def facebook
      @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
      @fb_user = FbGraph::User.fetch(request.env["omniauth.auth"].credentials.token).friends
      if !@user
        flash[:error] = 'This email address is already used in the system.'
        redirect_to :back
      elsif @user.persisted?
        flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind =&gt; "Facebook"
        sign_in_and_redirect @user, :event =&gt; :authentication
      else
        session["devise.facebook_data"] = request.env["omniauth.auth"]
        redirect_to new_user_registration_url
      end
    end
  </code></pre>
  
  <p>In <strong>User</strong> model:</p>
  
  <pre><code>  def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
      data = access_token.extra.raw_info
      if user = User.where(:provider =&gt; 'facebook', :uid =&gt; data.id).first
        user
      elsif user = User.where('email = ? AND provider IS NULL', data.email).first
        return false
      else
        ...saving data...
      end
      return user if user
    end
  </code></pre>