Ruby on rails 如何在rails中访问Post方法请求的数据
我试图使用POST方法从某个API访问数据,但它返回了我发送的实际参数列表。这是我的代码,我不知道我是否做得对,我会很高兴你的帮助 这是我的控制器 输出Ruby on rails 如何在rails中访问Post方法请求的数据,ruby-on-rails,Ruby On Rails,我试图使用POST方法从某个API访问数据,但它返回了我发送的实际参数列表。这是我的代码,我不知道我是否做得对,我会很高兴你的帮助 这是我的控制器 输出 如何访问从API返回的实际数据?您正在使用输出到服务器控制台的puts。我不知道输出在哪里。您应该为自己设置一个与代码块所在的控制器操作同名的视图,例如,如果这是索引操作: def index params= { your_hash_keys: "value" } end 然后,您应该在应用程序/views/controller\u na
如何访问从API返回的实际数据?您正在使用输出到服务器控制台的
puts
。我不知道输出在哪里。您应该为自己设置一个与代码块所在的控制器操作同名的视图,例如,如果这是索引操作:
def index
params= { your_hash_keys: "value" }
end
然后,您应该在应用程序/views/controller\u name/中有一个index.html.erb,而不是在控制器中的put“Received::”+res.body.to\u yaml
中使用@debug=“Received:”+res.body.to\u yaml
并在视图中执行类似的输出操作
或者,不建议在控制器中内联渲染:
render inline: "Received:: " + res.body.to_yaml
您还应该重命名您的params变量,因为Rails将其用于传入参数。总而言之,我认为MVC教程是一个很好的开始
require "net/http"
require "uri"
uri = URI.parse('https://start.exactonline.nl/api/oauth2/token')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # You should use VERIFY_PEER in production
request.set_form_data({
"code" => "#{code}",
"redirect_uri" => '/auth/exact/callback',
"grant_type" => "authorization_code",
"client_id" => CLIENT_ID,
"client_secret" => CLIENT_SECRET
})
response = http.request(request)
然而,我将使用而不是重新发明Oauth轮子。很难做到正确。如果找不到现成的提供程序,则创建自定义提供程序非常简单:
require 'omniauth-oauth2'
module OmniAuth
module Strategies
class ExactOnline < OmniAuth::Strategies::OAuth2
# change the class name and the :name option to match your application name
option :name, :exactonile
option :client_options, {
:site => "https://start.exactonline.nl",
:authorize_url => "/api/oauth2/token"
}
uid { raw_info["id"] }
info do
{
:email => raw_info["email"]
# and anything else you want to return to your API consumers
}
end
def raw_info
@raw_info ||= access_token.get('/api/v1/me.json').parsed
end
end
end
end
需要'omniauth-oauth2'
模块OmniAuth
模块策略
类ExactOnline“https://start.exactonline.nl",
:authorize_url=>“/api/oauth2/token”
}
uid{raw_info[“id”]}
信息做
{
:email=>原始信息[“email”]
#以及您希望返回给API消费者的任何其他内容
}
结束
def原始信息
@raw_info | |=access_token.get('/api/v1/me.json')。已解析
结束
结束
结束
结束
他仍然没有实际发送请求-@max,你能给我提供一些示例,说明如何使用POST方法发送和接收数据吗?
,因为这就是我试图归档的内容我已经有了这个gem
[ExactOnline]()我用来验证用户身份的ExactOnlineAPI
如何使用它向exactOnline发送post请求。这就是我在omniauth.rbprovider:exact,{CLIENT_ID},'SECRET code'
嗯,我认为你不应该使用这个Gem。在自述文件的第一行中是这样说的,在尝试使用oauth进行身份验证之前,您应该首先学习Rails,因为它目前不适合您。stackoverflow不适用于一步一步的教程。有一个非常过时,但经过了基本的。好的,谢谢,但只是要明确一下,你从哪里得到http
variableresponse=http.request(request),因为我遇到了这个错误
未定义的局部变量或方法http',for
`Woops,我把这个例子粘在一起真的搞砸了。编辑
require "net/http"
require "uri"
uri = URI.parse('https://start.exactonline.nl/api/oauth2/token')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE # You should use VERIFY_PEER in production
request.set_form_data({
"code" => "#{code}",
"redirect_uri" => '/auth/exact/callback',
"grant_type" => "authorization_code",
"client_id" => CLIENT_ID,
"client_secret" => CLIENT_SECRET
})
response = http.request(request)
require 'omniauth-oauth2'
module OmniAuth
module Strategies
class ExactOnline < OmniAuth::Strategies::OAuth2
# change the class name and the :name option to match your application name
option :name, :exactonile
option :client_options, {
:site => "https://start.exactonline.nl",
:authorize_url => "/api/oauth2/token"
}
uid { raw_info["id"] }
info do
{
:email => raw_info["email"]
# and anything else you want to return to your API consumers
}
end
def raw_info
@raw_info ||= access_token.get('/api/v1/me.json').parsed
end
end
end
end