我是rails世界的新手,我正在尝试构建一个应用程序,只是允许我根据用户输入在亚马逊和这些网站上搜索内容.
我做了一点研究,似乎httparty宝石是一个很好的起点?到目前为止我发现的文件并不是最好的.他们并没有给我很多信息(在哪里放置代码等).
是否有任何现有的教程或代码示例,我可以使用或看一看,让我更好地了解这个东西是如何工作的?
在我的控制器中,我有以下代码......
response = HTTParty.get('https://graph.facebook.com/zuck')
logger.debug(response.body.id)
Run Code Online (Sandbox Code Playgroud)
我得到一个NoMethodError/undefined方法`id'
如果我做...
logger.debug(response.body)
Run Code Online (Sandbox Code Playgroud)
它按原样输出......
{"id":"4","name":"Mark Zuckerberg","first_name":"Mark","last_name":"Zuckerberg","link":"http:\/\/www.facebook.com\/zuck","username":"zuck","gender":"male","locale":"en_US"}
Run Code Online (Sandbox Code Playgroud)
人们会认为这是response.body.id,但显然这不起作用.提前致谢!
不知何故,HTTParty返回401 CURL正常工作.不确定如何在标头中传递令牌.
工作(200):
curl http://localhost:3020/api/products -H 'Authorization: Token token="111"'
Run Code Online (Sandbox Code Playgroud)
不工作(401):
HTTParty.get('http://localhost:3020/api/products', headers: {"Authorization: Token token" => '111'})
Run Code Online (Sandbox Code Playgroud)
我曾尝试只用"Authorization" => '111'和"token" => '111',但相同的结果.
我想查看HTTParty gem从我的参数构建的完整URL,无论是在提交之前还是之后,都没关系.
我也很乐意从响应对象中抓取这个,但我也看不到这样做的方法.
(背景)
我正在使用HTTParty gem为API构建一个包装器.这是广泛的工作,但偶尔我从远程站点得到一个意外的响应,我想深入了解为什么 - 这是我发送错误的东西?如果是这样,什么?我是否以某种方式违反了请求?查看原始URL对于故障排除很有帮助,但我看不出如何.
例如:
HTTParty.get('http://example.com/resource', query: { foo: 'bar' })
Run Code Online (Sandbox Code Playgroud)
大概产生:
http://example.com/resource?foo=bar
Run Code Online (Sandbox Code Playgroud)
但我怎么检查这个?
在一个例子中我这样做:
HTTParty.get('http://example.com/resource', query: { id_numbers: [1, 2, 3] }
Run Code Online (Sandbox Code Playgroud)
但它没有用.通过实验,我能够产生这样的工作:
HTTParty.get('http://example.com/resource', query: { id_numbers: [1, 2, 3].join(',') }
Run Code Online (Sandbox Code Playgroud)
很明显,HTTParty构成查询字符串的默认方法与API设计者的首选格式不一致.这没关系,但确切地知道需要什么是很尴尬的.
我正在尝试使用Ruby on Rails与Salesforce API进行通信.我可以轻松地获取数据,但我在向服务器发布数据时遇到问题.我按照Quinton Wall的帖子使用HTTParty:
但我似乎能够从salesforce服务器获得的是我以html身份提交正文的错误
{"message"=>"此资源不支持'application/x-www-form-urlencoded'的MediaType","errorCode"=>"UNSUPPORTED_MEDIA_TYPE"}
负责的代码如下:
require 'rubygems'
require 'httparty'
class Accounts
include HTTParty
format :json
...[set headers and root_url etc]
def self.save
Accounts.set_headers
response = (post(Accounts.root_url+"/sobjects/Account/", :body => {:name => "graham"}.to_json))
end
end
Run Code Online (Sandbox Code Playgroud)
任何人都知道为什么身体应该被发布为html以及如何改变这一点,以便它肯定像json一样,以便salesforce不拒绝它?
任何帮助,将不胜感激.干杯
我很难使用Ruby的HTTParty库向API端点发出POST请求.我正在与之交互的API是Gittip API,它们的端点需要身份验证.我已经能够使用HTTParty成功地进行经过身份验证的GET请求.
您可以在示例代码中看到:
user = "gratitude_test"
api_key = "5962b93a-5bf7-4cb6-ae6f-aa4114c5e4f2"
# I have included real credentials since the above is merely a test account.
HTTParty.get("https://www.gittip.com/#{user}/tips.json",
{ :basic_auth => { :username => api_key } })
Run Code Online (Sandbox Code Playgroud)
该请求有效并按预期返回以下内容:
[
{
"amount" => "1.00",
"platform" => "gittip",
"username" => "whit537"
},
{
"amount" => "0.25",
"platform" => "gittip",
"username" => "JohnKellyFerguson"
}
]
Run Code Online (Sandbox Code Playgroud)
但是,我无法使用HTTParty成功发出POST请求.Gittip API描述了使用curl发出POST请求,如下所示:
curl https://www.gittip.com/foobar/tips.json \
-u API_KEY: \
-X POST \
-d'[{"username":"bazbuz", "platform":"gittip", "amount": "1.00"}]' \
-H"Content-Type: application/json"
Run Code Online (Sandbox Code Playgroud)
我尝试使用HTTParty(不成功)构建我的代码,如下所示:
user …Run Code Online (Sandbox Code Playgroud) 我想在不使用代理的情况下捕获给定操作的完整请求(raw_request - 线路上的内容).
我知道Class上的debug_output方法,这可能是解决方案的一部分.但不清楚如何根据每个请求设置它.
考虑以下 ...
@response = HTTParty.post(@job.callback_url, body: @job.to_json)
notification = Notification.new
notification.response_body = @response.body
notification.response_code = @response.code
notification.request_body = ????
Run Code Online (Sandbox Code Playgroud)
谢谢!
乔纳森
我可以通过以下方式从Oauth2 API获取信息:
token = "Token I get from authenticating my App"
auth = "Bearer " + token
user = HTTParty.get("API Website", :headers => { "Authorization" => auth})
Run Code Online (Sandbox Code Playgroud)
我将如何发布到我的应用中生成的API内容?我有一个实例变量:
@contact = {"contact": {"name": "John Doe" }}
Run Code Online (Sandbox Code Playgroud)
我试过这个:
token = "Token I get from authenticating my App"
auth = "Bearer " + token
user = HTTParty.get("API Website", :headers => { "Authorization" => auth}, @contact)
Run Code Online (Sandbox Code Playgroud)
无济于事.
我正在使用 HTTParty gem 调用 GitHub API 以访问用户的存储库列表。
这是一个使用 Sinatra 的非常简单的应用程序,它根据用户存储库中出现的最常用语言显示用户最喜欢的编程语言。
我对如何编写一个模拟实际 API 调用的 RSpec 期望值有点困惑,而只是检查是否正在返回 json 数据。
我有一个模拟 .json 文件,但不确定如何在我的测试中使用它。
有任何想法吗?
github_api.rb
require 'httparty'
class GithubApi
attr_reader :username, :data, :languages
def initialize(username)
@username = username
@response = HTTParty.get("https://api.github.com/users/#{@username}/repos")
@data = JSON.parse(@response.body)
end
end
Run Code Online (Sandbox Code Playgroud)
github_api_spec.rb
require './app/models/github_api'
require 'spec_helper'
describe GithubApi do
let(:github_api) { GithubApi.new('mock_user') }
it "receives a json response" do
end
end
Run Code Online (Sandbox Code Playgroud)
为清楚起见,其余文件:
结果.rb
require 'httparty'
require_relative 'github_api'
class Results
def initialize(github_api = Github.new(username))
@github_api = github_api
@languages = …Run Code Online (Sandbox Code Playgroud) 是否可以在HTTParty中使用OAuth?我正在尝试执行此 API调用,但与文档相矛盾,它需要身份验证.
在你说"使用特定于Twitter的宝石"之前,请听我说 - 我试过了.我尝试了twitter,grackle和无数其他人,但没有人支持这个特定的API调用.所以,我转向了HTTParty.
那么,我如何在HTTParty中使用OAuth?
httparty ×10
ruby ×7
curl ×2
access-token ×1
oauth ×1
oauth-2.0 ×1
rspec ×1
salesforce ×1
service ×1
web ×1