使用 Ruby + OAuth 访问 Yelp API

发布于 2024-10-02 06:47:16 字数 718 浏览 4 评论 0原文

我刚刚开始使用 OAuth,我尝试制作一个小型客户端来连接到一些 Web 服务...我尝试了 twitter,它的工作方式就像一个魅力,但是,我也尝试访问 Yelp V2 API(遵循他们的 Python 示例),但是我总是回复: HTTP 400错误请求

缺少参数:oauth_consumer_key

这是我的代码:

require 'rubygems'
require 'oauth'

CONSUMER_KEY = "MY_CONSUMER_KEY"
SECRET = "MY_CONSUMER_SECRET"
TOKEN = "MY_TOKEN"
TOKEN_SECRET = "MY_TOKEN_SECRET"


consumer = OAuth::Consumer.new( CONSUMER_KEY,SECRET, {:site => "http://api.yelp.com", :signature_method => "HMAC-SHA1", :scheme => :header})

access_token = OAuth::AccessToken.new( consumer, TOKEN,TOKEN_SECRET)

p access_token.get("/v2/search?location=new+york").body

无论如何,该代码与twitter API一起使用没有任何问题(我实际上遵循了twitter的示例代码)

提前干杯并感谢, 泽

I just getting started with OAuth, and I tried to make a small client to connect to some webservices... I tried twitter and it worked like a charm, however, I also tried to access Yelp V2 API (following their Python example) but I always get back as an answers:
HTTP 400 Bad Request

Missing parameter: oauth_consumer_key

Here's my code:

require 'rubygems'
require 'oauth'

CONSUMER_KEY = "MY_CONSUMER_KEY"
SECRET = "MY_CONSUMER_SECRET"
TOKEN = "MY_TOKEN"
TOKEN_SECRET = "MY_TOKEN_SECRET"


consumer = OAuth::Consumer.new( CONSUMER_KEY,SECRET, {:site => "http://api.yelp.com", :signature_method => "HMAC-SHA1", :scheme => :header})

access_token = OAuth::AccessToken.new( consumer, TOKEN,TOKEN_SECRET)

p access_token.get("/v2/search?location=new+york").body

Regardless to say, that code works with twitter API without any problem (I actually followed twitter's example code)

Cheers and thanks in advance,
Ze

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女净妖师 2024-10-09 06:47:16

使用 :query_string 而不是 :header 一切都会起作用(至少对我来说)。

Use :query_string instead of :header and everything will work (at least for me).

沫雨熙 2024-10-09 06:47:16

相同的代码,使用 Signet

require 'signet/oauth_1/client'
client = Signet::OAuth1::Client.new(
  :consumer_key =>        'MY_CONSUMER_KEY',
  :consumer_secret =>     'MY_CONSUMER_SECRET',
  :access_token_key =>    'MY_TOKEN_KEY',
  :access_token_secret => 'MY_TOKEN_SECRET'
)
response = client.fetch_protected_resource(
  :uri => 'http://api.yelp.com/v2/search?location=new+york'
)
# The Rack response format is used here
status, headers, body = response
p body

按照 Yelp 文档 中,OAuth 参数不必必须在查询字符串中传递。事实上,接受的答案解决了这个问题,这向我表明,oauth gem 中可能存在导致此问题的错误。

Same code, using Signet:

require 'signet/oauth_1/client'
client = Signet::OAuth1::Client.new(
  :consumer_key =>        'MY_CONSUMER_KEY',
  :consumer_secret =>     'MY_CONSUMER_SECRET',
  :access_token_key =>    'MY_TOKEN_KEY',
  :access_token_secret => 'MY_TOKEN_SECRET'
)
response = client.fetch_protected_resource(
  :uri => 'http://api.yelp.com/v2/search?location=new+york'
)
# The Rack response format is used here
status, headers, body = response
p body

As per the Yelp documentation, the OAuth parameters do not have to be passed in the query string. The fact that the accepted answer resolved the issue indicates to me that there's probably a bug in the oauth gem causing this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文