JSON parameters not available in request hash (Rails, omniauth-google-oauth2 gem) -


overview

i want implement google oauth2 server side (hybrid) omniauth google oauth2 gem (0.2.6). however, code parameter send app not added request.params hash. thus, omniauth throws error, can't find code.

details

after retrieving auth code google send server (by ajax):

// send code server     $.ajax({       type: 'post',       url: '/auth/google_oauth2/callback',       contenttype: 'application/json',       success: function(result) {         // handle or verify server response.       },       processdata: false,       data: json.stringify(password_result)     }); 

this throws error:

"error" : "invalid_request", "error_description" : "missing required parameter: code" 

after going through stack, figured out following:

  1. as long have 'application/json' set content type, rack parses params correctly , env object contains parsed parameters:

    "action_dispatch.request.request_parameters"=>{"code"=>"<sent_in_code>"} 
  2. however, request.params hash remains empty. since omniauth checks request.params['code'], source of error.

  3. request.post empty, looking @ source code of rack underlying cause empty request.params hash.

  4. when sending code in standard format data:"code="+authresult['code'], parameter available in request.params hash. (i strange undefined route error then, different issue.)

questions

now, though can avoid issue not using json, i'm still intereted in answers following questions:

  1. why code parameter not available in request.post/request.params, though gets parsed correctly?
  2. is there way fix this, can still send auth code in json app?

i've spent 2 afternoons trying answers myself, haven't gotten conclusion far.

omniauth-google-oauth2 tries auth code rack::request.params hash. however, rack apparently not have json parsing built-in. params calls post calls form_data? looks application/x-www-form-urlencoded or multipart/form-data. tries parseable_data?, not parse json either. seems rack not support json out of box, @ this answer.

the "action_dispatch.request.request_parameters"=>{"code"=>"<sent_in_code>"} hash works because done rails actiondispatch::request, subclasses rack::request. because omniauth gem included in app rack middleware, not know of actiondispatch request object.

the question remains why this example uses json.

possible solutions:

  • money-patch json support rack - not recommended
  • just use application/x-www-form-urlencoded - recommended

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -