user361526
user361526

Reputation: 3383

Using Ruby + OAuth to access Yelp API

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

Upvotes: 4

Views: 2425

Answers (2)

Bob Aman
Bob Aman

Reputation: 33239

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.

Upvotes: 0

Oscar Del Ben
Oscar Del Ben

Reputation: 4515

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

Upvotes: 2

Related Questions