title: Ember-Cli, Rails and Torii Session 400 Bad Request
body: <p>I am new to ember-cli, currently I am having trouble with oauth2 with LinkedIn, the server side is rails.  This is also my first question so please let me know if there's any other information needed or if it's too verbose.  When i attempt to sign in I get the linkedin popup but then get the following error:</p>
  
  <p><code>POST http://localhost:9000/v1/sessions 400 (Bad Request)
  jQuery.ajaxTransport.send @ jquery.js:9664jQuery.extend.ajax @ jquery.js:9215exports.default.Ember.default.Object.extend._fetchSession @ application.js:22initializePromise @ ember.debug.js:45486Promise @ ember.debug.js:47114_fetchSession @ application.js:21fetch @ application.js:15(anonymous function) @ session.js:72tryCatch @ ember.debug.js:45439invokeCallback @ ember.debug.js:45451(anonymous function) @ ember.debug.js:47350(anonymous function) @ ember.debug.js:26472Queue.invoke @ ember.debug.js:878Queue.flush @ ember.debug.js:943DeferredActionQueues.flush @ ember.debug.js:748Backburner.end @ ember.debug.js:173Backburner.run @ ember.debug.js:228Backburner.join @ ember.debug.js:247run.join @ ember.debug.js:15904run.bind @ ember.debug.js:15966jQuery.Callbacks.fire @ jquery.js:3148jQuery.Callbacks.self.fireWith @ jquery.js:3260jQuery.extend.ready @ jquery.js:3472completed @ jquery.js:3503</code></p>
  
  <hr>
  
  <p>I get the same response when I actually hit the 'sign in with linkedin' button.</p>
  
  <p>Here is my code for ember-cli:</p>
  
  <h2>app/adapters/application.js</h2>
  
  <pre><code>    import DS from 'ember-data';
  
  export default DS.RESTAdapter.extend({
    namespace: 'v1'
  });
  </code></pre>
  
  <hr>
  
  <h2>app/routes/application.js</h2>
  
  <pre><code>    import Ember from 'ember';
  //takes place of App.IndexRoute =
  export default Ember.Route.extend({
    //allow session to persist after page refreshes &amp; fetch wehn activated
    //shoudl only happen once
    activate: function() {
      this.get('session').fetch();
    },
  
    actions: {
      signInViaLinkedin: function(){
        var route = this;
  
         this.get('session').open('linked-in-oauth2').then(function(authorization){
          // do the things after login, like redirect to dashboard
        }, function(error) {
          route.controller.set('error', 'Could not sign you in: ' + error.message);
        });
      }
    }
  });
  </code></pre>
  
  <hr>
  
  <h2>config/environment.js</h2>
  
  <pre><code>    module.exports = function(environment) {
    var ENV = {
      modulePrefix: 'fetch',
      environment: environment,
      baseURL: '/',
      locationType: 'auto',
      EmberENV: {
        FEATURES: {
          // Here you can enable experimental features on an ember canary build
          // e.g. 'with-controller': true
        }
      },
  
      APP: {
        // Here you can pass flags/options to your application instance
        // when it is created
      }
    };
    ...
    if (environment === 'development') {
      // ENV.APP.LOG_RESOLVER = true;
      ENV.APP.LOG_ACTIVE_GENERATION = true;
      // ENV.APP.LOG_TRANSITIONS = true;
      // ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
      ENV.APP.LOG_VIEW_LOOKUPS = true;
      ENV.puppifi = {
        sessionUrl: 'http://localhost:9000/v1/sessions'
      };
  
      ENV.torii = {
        sessionServiceName: 'session',
        providers: {
          'linked-in-oauth2': {
            apiKey: 'LINKED_IN_CLIENT_ID',
            redirectUri: 'http://localhost:4200',
          }
        }
      };
  
    if (environment === 'test') {
      // Testem prefers this...
      ENV.baseURL = '/';
      ENV.locationType = 'none';
  
      // keep test console output quieter
      ENV.APP.LOG_ACTIVE_GENERATION = false;
      ENV.APP.LOG_VIEW_LOOKUPS = false;
  
      ENV.APP.rootElement = '#ember-testing';
    }
  
    if (environment === 'production') {
      ENV.torii = {
        sessionServiceName: 'session',
        providers: {
          'linked-in-oauth2': {
            apiKey: 'LINKED_IN_CLIENT_ID',
            redirectUri: 'http://http://development.fetch.divshot.io',
          }
        }
      };
      ENV.puppifi ={
        sessionUrl: '/__/proxy/v1/sessions'
      };
    }
    return ENV;
    }
  };
  </code></pre>
  
  <hr>
  
  <h1>Rails App</h1>
  
  <hr>
  
  <h2>app/controller/v1/sessions_controller.rb</h2>
  
  <pre><code>module V1
    # session controller can fetch old sessions 
    class SessionsController &lt; ApplicationController
      # skipped because ember and rails are on different domains
      skip_before_action :verify_authenticity_token
  
      def create
        linkedin_authenticator = LinkedinAuthenticator.new(linkedin_auth_code)
        user_factory = UserFactory.new(linkedin_authenticator)
        user_factory.find_or_create_user
  
        render json: user, status: :created
      end
  
  
      private
  
      def linkedin_auth_code 
        params.require(:'linkedin_auth_code')
      end
    end
  </code></pre>
  
  <h2>    end</h2>
  
  <h2>services/linkedin_authenticator.rb</h2>
  
  <pre><code>require "net/http"
  require "net/https"
  
  class LinkedinAuthenticator 
    LINKED_IN_OAUTH_PATH = "https://linkedin.com/uas/oauth2/authorization?response_type=code"
  
    def initialize(auth_code)
      @auth_code = auth_code
    end
  
    def name
      linkedin_user[:login]
    end
  
  
    private
  
    def linkedin_user
      @linkedin_user ||= linkedin_client.user
    end
  
    def linkedin_client
      OAuth2::Client.new(access_token: access_token)
    end
  
    def access_token
      linkedin_response["access_token"]
    end
  
    def token_type
      linkedin_response["token_type"]
    end
  
    def scope
      linkedin_response["scope"]
    end
  
    def linkedin_response
      @linkedin_response ||= JSON.parse(res.body)
    end
  
    def res
      http.request(req)
    end
  
    def req
      req = Net::HTTP::Post.new(uri.path)
      req.set_form_data(post_data)
      req["Accept"] = "application/json"
      req
    end
  
    def http
      http = Net::HTTP.new(uri.host, uri.port)
      http.use_ssl = true
      http
    end
  
    def uri
      URI.parse(LINKED_IN_OAUTH_PATH)
    end
  
    def post_data
      {
        "client_id" =&gt; ENV["LINKED_IN_CLIENT_ID"],
        "client_secret" =&gt; ENV["LINKED_IN_CLIENT_SECRET"],
        "code" =&gt; @auth_code
      }
    end
  end
  </code></pre>