简体   繁体   中英

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>

<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(\''+@code+'\',\''+@cc_id+'\',\''+@cc_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 => something,
:redirect_uri => "http%3A%2F%2Fopenhealthdesigns.com%2Frktest"}}

I am using respond_to :json at the top of a controller file with respond_with @notes in the method that I want to return the json, and it works well.

The app uses Ajax to return data for an search feature, but I don't think how the data is being called should make a difference.

A litte more code for context:

class NotesController < ApplicationController
  require 'coderay'
  respond_to :json                                                                                                                                                                                                         


 def search
  @notes = Note.fulltext_search(params[:title])
  @notes.each do |n|
    n.content = CodeRay.scan(n.content, :ruby).div(:css => :class)
  end
  respond_with @notes
 end

API Docs - depending on how the method is being called. If using .json, a respond_to block would work also.

respond_with

respond_to

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM