How to send a post request and receive json back in rails

I’ve looked through a fair amount of the questions - yet, I am still
confused.

I am making a Rails app that connects with the Healthgraph API (here
http://developer.runkeeper.com/healthgraph/registration-authorization).

I am supposed to make a POST request via Rails - based on a parameter
I recieve and then recieve the JSON. I have tried the outh2, runkeeper
and other gems and they don’t work because of a certain dependency.
However, I am lost on how to POST via Rails and recieve a response.

I do get the code back in my controller - however, I still don’t know
how to get an access token.

def rktest

respond_to

if params[:code]
  require 'oauth2'

  @code=params[:code]

  @cc_id=client_id
  @client_s=client_secret

else

  @code="None"

end

I’m trying to also do it via Javascript - but I’d rather not - since
it leaves my client secret exposed. Also, I don’t recieve data back
either.

<script type="text/javascript" src="javascripts/

jquery-1.7.1.min.js">

<script type="text/javascript">

function get_access_token(code,ccid,cc_s){

$.ajaxSetup({
'beforeSend': function(xhr){
xhr.setRequestHeader("Accept", "text/javascript")
}
});

var sent = { 'grant_type': 'authorization_code',
'code': code,
'client_id': ccid,
'client_secret': cc_s,
'redirect_uri': 'http://myurl.com/rktest'
};

document.write("<p>" + sent + "</p>");

$.ajax({
url: 'https://runkeeper.com/apps/token',
data: sent,
type: 'POST',
dataType: 'json',
success: function(data, status, xhr){
if (status === 'error' || !xhr.responseText){
handleError();
}else{

document.write("<p>" + data + "</p>");
document.write("<p>" + status + "</p>");
document.write("<p>" + xhr + "</p>");

}
}
});

}

<%= 'get_access_token(\''[email protected]+'\',\''[email protected]_id+'\',\''[email protected]_s

+’’);’ %>

Also, here is my routes file currently:

match '/rktest', :to => 'main#rktest', :via => [:get, :post]

Even this does not work.

RestClient.post 'https://runkeeper.com/apps/token', {:params => {
    :grant_type => 'authorization_code',
    :code => @code,
    :client_id => '8e9b36478b764ac38ef1bdabc6d14d60',
    :client_secret => 'fb66f35c944246588db30e1288501315',

:redirect_uri => “http%3A%2F%2Fopenhealthdesigns.com%2Frktest”}}

Archit Bhise wrote in post #1041310:

I’m trying to also do it via Javascript - but I’d rather not - since
it leaves my client secret exposed. Also, I don’t recieve data back
either.



$.ajaxSetup({
‘beforeSend’: function(xhr){
xhr.setRequestHeader(“Accept”, “text/javascript”)
}
});

Here’s an decent explanation of why you JavaScript solution will not
work:

http://www.jquery-tutorial.net/ajax/same-origin-policy/

The same origin policy will prevent your did sending a request directly
to another domain. Look into JSONP for a workaround to the same origin
policy.

You should, however, be able to send the request from the server side
using Ruby.

See open-uri for more details. Here’s one example that I found via
Google:

http://juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs