Despite going through numerous questions, I am still perplexed.
My current project involves creating a Rails application that interacts with the Healthgraph API (link here ).
I am required to send a POST request using Rails based on a received parameter and then handle the JSON response. I attempted to use various gems like oauth2 and runkeeper, but they failed due to specific dependencies. My challenge lies in properly executing a POST request via Rails and managing the returned information.
While I do receive the code back in my controller, acquiring an access token remains elusive.
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've also experimented with Javascript for this task, which raises concerns about exposing sensitive client secrets. Additionally, I've been unsuccessful in receiving data back through this method as well.
<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+'\');' %>
Here is the current content of my routes file:
match '/rktest', :to => 'main#rktest', :via => [:get, :post]
Unfortunately, even after implementing this configuration, the desired outcome is not achieved.
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"}}