1

I'm new to Ruby, therefore, not much familiar with the syntaxes -

I want a Ruby code for the following curl request -

curl -X POST http://<auth_server_endpoint>/oauth/token -d "grant_type
=client_credentials" -H"Authorization: Basic encode64Of(clientid:secret)"

After following the link - How to invoke HTTP POST method over SSL in ruby?

I've written a function to get the response -

def getToken(client_id, secret)

encodedId = Base64.encode64(client_id+":"+secret)

uri = URI.parse("auth_server_endpoint")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new("/oauth/token")
request.add_field('Content-Type', 'application/json')
request.add_field('Authorization', "Basic #{encodedId}")

//FAILS BELOW

response = http.request(request, {'grant_type' => 'client_credentials'})
parsed_json = JSON.parse(response)

self.access_token = parsed_json.fetch("access_token")
self.expires_in = parsed_json.fetch("expires_in")

end

I'm not able to figure out the error. Kindly suggest the changes I need to make.

ERROR LOG -

OpenSSL::SSL::SSLError - SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol:
    /netdir/http.rb:920:in `connect'
    /netdir/http.rb:920:in `block in connect'
    /rubydir/2.1.0/timeout.rb:76:in `timeout'
    /netdir/http.rb:920:in `connect'
    /netdir/http.rb:863:in `do_start'
    /netdir/http.rb:852:in `start'
    /netdir/http.rb:1369:in `request'
    /rubydir/gems/2.1.0/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:in `request'
    /appdir/lib/sc_customer/auth_token.rb:30:in `getToken'
    /appdir/controllers/payment_controller.rb:276:in `block (2 levels) in <module:API>'
    /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/probes/sinatra_probe.rb:24:in `call'
    /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/probes/sinatra_probe.rb:24:in `block in bind'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:569:in `call'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:569:in `block in route'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `[]'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block (3 levels) in process_destination_path'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block (2 levels) in process_destination_path'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `catch'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block in process_destination_path'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:25:in `instance_eval'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:25:in `process_destination_path'
    (eval):16:in `block (2 levels) in inject_root_methods'
    (eval):6:in `catch'
    (eval):6:in `block in inject_root_methods'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router/node/root.rb:554:in `[]'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router/node/root.rb:13:in `[]'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `block in call'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `catch'
    /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `call'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:910:in `route!'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:900:in `dispatch!'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/logger.rb:15:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/session/abstract/id.rb:195:in `context'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/session/abstract/id.rb:190:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/head.rb:9:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/methodoverride.rb:24:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/sc_core/logger.rb:58:in `call'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:83:in `block in call'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:76:in `each'
    /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:76:in `call'
    /rubydir/gems/2.1.0/gems/sc-mq-0.0.86/lib/sc_mq/rack/filter.rb:24:in `call'
    /appdir/lib/rack/bu_aware_request_handler.rb:32:in `block in call'
    /rubydir/gems/2.1.0/gems/activerecord_use_slave-0.0.5/lib/activerecord_use_slave.rb:44:in `using_connection'
    /appdir/lib/rack/bu_aware_request_handler.rb:31:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/connection_release.rb:11:in `call'
    /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/rack-metrics/rack.rb:11:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/serial_initializer.rb:49:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/log_exceptions.rb:11:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/transaction_tracer.rb:23:in `call'
    /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/sc_core/rack/filter.rb:14:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/lint.rb:48:in `_call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/lint.rb:36:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/showexceptions.rb:24:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/commonlogger.rb:20:in `call'
    /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/chunked.rb:41:in `call'
    /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/content_length.rb:14:in `call'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:552:in `process_client'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:632:in `worker_loop'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:142:in `start'
    /rubydir/gems/2.1.0/gems/unicorn-4.6.3/bin/unicorn:126:in `<top (required)>'
    /b2cdir/bin/unicorn:16:in `load'
    /b2cdir/bin/unicorn:16:in `<main>'
  ERROR -17/May/2016 10:56:42 [payment-b2c TXN-af2c9519-6e89-4fa2-8913-ee164f53523d REQ-28485d83-47a3-47cc-814d-993dc544697a] - OpenSSL::SSL::SSLError - SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol:
 /netdir/http.rb:920:in `connect'
 /netdir/http.rb:920:in `block in connect'
 /rubydir/2.1.0/timeout.rb:76:in `timeout'
 /netdir/http.rb:920:in `connect'
 /netdir/http.rb:863:in `do_start'
 /netdir/http.rb:852:in `start'
 /netdir/http.rb:1369:in `request'
 /rubydir/gems/2.1.0/gems/rest-client-1.6.7/lib/restclient/net_http_ext.rb:51:in `request'
 /appdir/lib/sc_customer/auth_token.rb:30:in `getToken'
 /appdir/controllers/payment_controller.rb:276:in `block (2 levels) in <module:API>'
 /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/probes/sinatra_probe.rb:24:in `call'
 /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/probes/sinatra_probe.rb:24:in `block in bind'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:569:in `call'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:569:in `block in route'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `[]'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block (3 levels) in process_destination_path'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:851:in `route_eval'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block (2 levels) in process_destination_path'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `catch'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:51:in `block in process_destination_path'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:25:in `instance_eval'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:25:in `process_destination_path'
 (eval):16:in `block (2 levels) in inject_root_methods'
 (eval):6:in `catch'
 (eval):6:in `block in inject_root_methods'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router/node/root.rb:554:in `[]'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router/node/root.rb:13:in `[]'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `block in call'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `catch'
 /rubydir/gems/2.1.0/gems/http_router-0.10.2/lib/http_router.rb:119:in `call'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:910:in `route!'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/application/routing.rb:900:in `dispatch!'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `block in call!'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `block in invoke'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `catch'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:921:in `invoke'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:769:in `call!'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:755:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/logger.rb:15:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/session/abstract/id.rb:195:in `context'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/session/abstract/id.rb:190:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/head.rb:9:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/methodoverride.rb:24:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/sc_core/logger.rb:58:in `call'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `block in call'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1471:in `synchronize'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:1389:in `call'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:83:in `block in call'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:76:in `each'
 /rubydir/gems/2.1.0/gems/padrino-core-0.10.5.patched/lib/padrino-core/router.rb:76:in `call'
 /rubydir/gems/2.1.0/gems/sc-mq-0.0.86/lib/sc_mq/rack/filter.rb:24:in `call'
 /appdir/lib/rack/bu_aware_request_handler.rb:32:in `block in call'
 /rubydir/gems/2.1.0/gems/activerecord_use_slave-0.0.5/lib/activerecord_use_slave.rb:44:in `using_connection'
 /appdir/lib/rack/bu_aware_request_handler.rb:31:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/connection_release.rb:11:in `call'
 /rubydir/gems/2.1.0/gems/rack-metrics-0.0.27/lib/rack-metrics/rack.rb:11:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/serial_initializer.rb:49:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/log_exceptions.rb:11:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/rack/transaction_tracer.rb:23:in `call'
 /rubydir/gems/2.1.0/gems/sc-core-1.1.36/lib/sc_core/rack/filter.rb:14:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/lint.rb:48:in `_call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/lint.rb:36:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/showexceptions.rb:24:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/commonlogger.rb:20:in `call'
 /rubydir/gems/2.1.0/gems/sinatra-1.3.3/lib/sinatra/base.rb:136:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/chunked.rb:41:in `call'
 /rubydir/gems/2.1.0/gems/rack-1.3.6/lib/rack/content_length.rb:14:in `call'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:552:in `process_client'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:632:in `worker_loop'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/lib/unicorn/http_server.rb:142:in `start'
 /rubydir/gems/2.1.0/gems/unicorn-4.6.3/bin/unicorn:126:in `<top (required)>'
 /b2cdir/bin/unicorn:16:in `load'
 /b2cdir/bin/unicorn:16:in `<main>'
  DEBUG -17/May/2016 10:56:42 [payment-b2c TXN-af2c9519-6e89-4fa2-8913-ee164f53523d REQ-28485d83-47a3-47cc-814d-993dc544697a] - POST [1.085s DB: 0.009s ( 0.8%) 7 queries] /payments - 500 Internal Server Error
Community
  • 1
  • 1
  • Which error? Please paste the entire error returned, including all backtrace. If it's an HTTP failure, please also post the HTTP headers returned. You can edit your question to add these details by clicking the `edit` button directly below your question (above this comment). – Michael Gaskill May 16 '16 at 23:33
  • @MichaelGaskill, I've added the logs. The rubydir/ , appdir/ are the locations of the directories. The line30 of getToken is - response = http.request(request, {'grant_type' => 'client_credentials'}) – user3554962 May 17 '16 at 05:42
  • You should try some of the things suggested on this page: [Unable to establish SSL connection, how do I fix my SSL cert?](http://stackoverflow.com/questions/15166950/unable-to-establish-ssl-connection-how-do-i-fix-my-ssl-cert). It's the same error that you get with OpenSSL: unknown protocol. The server that you're trying to communicate with doesn't have SSL running on port 443 or they're using a version of SSLL or TLS that's not compatible with the SSL version that you're using. – Michael Gaskill May 17 '16 at 06:06

0 Answers0