Yelp API Example

Prerequisite: When using Eclipse import:
scribe-1.1.0.jar
commons-code-1.4.jar

Create 2 files Yelp.java and YelpApi2.java as seen bellow

/*
 Example code based on code from Nicholas Smith at http://imnes.blogspot.com/2011/01/how-to-use-yelp-v2-from-java-including.html
 For a more complete example (how to integrate with GSON, etc) see the blog post above.
 */

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.scribe.builder.ServiceBuilder;
import org.scribe.model.OAuthRequest;
import org.scribe.model.Response;
import org.scribe.model.Token;
import org.scribe.model.Verb;
import org.scribe.oauth.OAuthService;

/**
 * Example for accessing the Yelp API.
 */
public class Yelp {

  OAuthService service;
  Token accessToken;

  /**
   * Setup the Yelp API OAuth credentials.
   *
   * OAuth credentials are available from the developer site, under Manage API access (version 2 API).
   *
   * @param consumerKey Consumer key
   * @param consumerSecret Consumer secret
   * @param token Token
   * @param tokenSecret Token secret
   */
  public Yelp(String consumerKey, String consumerSecret, String token, String tokenSecret) {
    this.service = new ServiceBuilder().provider(YelpApi2.class).apiKey(consumerKey).apiSecret(consumerSecret).build();
    this.accessToken = new Token(token, tokenSecret);
  }

  /**
   * Search with term and location.
   *
   * @param term Search term
   * @param latitude Latitude
   * @param longitude Longitude
   * @return JSON string response
   */
  public String search(String term, double latitude, double longitude) {
    OAuthRequest request = new OAuthRequest(Verb.GET, "http://api.yelp.com/v2/search");
    request.addQuerystringParameter("term", term);
    request.addQuerystringParameter("ll", latitude + "," + longitude);
    this.service.signRequest(this.accessToken, request);
    Response response = request.send();
    return response.getBody();
  }

  // CLI
  public static void main(String[] args) {
    // Update tokens here from Yelp developers site, Manage API access.
    String consumerKey = "";
    String consumerSecret = "";
    String token = "";
    String tokenSecret = "";

    Yelp yelp = new Yelp(consumerKey, consumerSecret, token, tokenSecret);
    String response = yelp.search("burritos", 30.361471, -87.164326);

    System.out.println(response);
  }
}
import org.scribe.model.Token;
import org.scribe.builder.api.DefaultApi10a;

/**
 * Service provider for "2-legged" OAuth10a for Yelp API (version 2).
 */
public class YelpApi2 extends DefaultApi10a {

  @Override
  public String getAccessTokenEndpoint() {
    return null;
  }

  @Override
  public String getAuthorizationUrl(Token arg0) {
    return null;
  }

  @Override
  public String getRequestTokenEndpoint() {
    return null;
  }

}

Run as Application

Result >>

{"region":{"span":{"latitude_delta":0.18219147031249605,"longitude_delta":0.27610887841802878},"center":{"latitude":30.444285304687501,"longitude":-87.191894035644552}},"total":9,"businesses":[{"distance":13281.016303705661,"mobile_url":"http://m.yelp.com/biz/kus0gkFvgLl31XqvinHpxQ","rating_img_url":"http://media2.ak.yelpcdn.com/static/201012164084228337/img/ico/stars/stars_4.png","review_count":7,"name":"Moe's Southwest Grill","rating":4.0,"url":"http://www.yelp.com/biz/moes-southwest-grill-pensacola","location":{"city":"Pensacola","display_address":["5100 N 9th Ave","Pensacola, FL 32504"],"geo_accuracy":8,"postal_code":"32504","country_code":"US","address":["5100 N 9th Ave"],"coordinate":{"latitude":30.4736242,"longitude":-87.211959699999994},"state_code":"FL"},"phone":"8508570202","snippet_text":"Pretty good. I like being able to choose exactly the way I want on my food.  They have tasty cheese dip too.  Kids eat free on Tuesdays which is great if...","image_url":"http://s3-media2.ak.yelpcdn.com/bphoto/ZzdUC0rYIyxYTn3b2IC3Tg/ms.jpg","snippet_image_url":"http://s3-media2.ak.yelpcdn.com/photo/d8_-Rkjtcaw9T5t83-DqYg/ms.jpg","display_phone":"+1-850-857-0202","rating_img_url_large":"http://media4.ak.yelpcdn.com/static/20101216169592178/img/ico/stars/stars_large_4.png","id":"moes-southwest-grill-pensacola","categories":[["Mexican","mexican"]],"rating_img_url_small":"http://media2.ak.yelpcdn.com/static/20101216418129184/img/ico/stars/stars_small_4.png"},{"distance":13066.321052217352,"mobile_url":"http://m.yelp.com/biz/sTle6jQCVR8kb_Qt0Kx-nw","rating_img_url":"http://media3.ak.yelpcdn.com/static/201012161235323114/img/ico/stars/stars_3_half.png","review_count":7,"name":"Tijuana Flats Burrito Company","rating":3.5,"url":"http://www.yelp.com/biz/tijuana-flats-burrito-company-pensacola","location":{"city":"Pensacola","display_address":["4970 Bayou Blvd","Pensacola, FL 32503"],"geo_accuracy":8,"postal_code":"32503","country_code":"US","address":["4970 Bayou Blvd"],"coordinate":{"latitude":30.472332999999999,"longitude":-87.209502999999998},"state_code":"FL"},"phone":"8504762200","snippet_text":"There aren't many good Mexican restaurants in the Pensacola, FL area, but this one does fill a niche in the Mexican theme. It is a fast version of \"not full...","image_url":"http://s3-media1.ak.yelpcdn.com/bphoto/MEqt4Ckg9sQuUPo0Xld8ww/ms.jpg","snippet_image_url":"http://s3-media3.ak.yelpcdn.com/photo/ySNtPKAsdU1Nw3Z03w2uag/ms.jpg","display_phone":"+1-850-476-2200","rating_img_url_large":"http://media1.ak.yelpcdn.com/static/201012161161255655/img/ico/stars/stars_large_3_half.png","id":"tijuana-flats-burrito-company-pensacola","categories":[["Mexican","mexican"]],"rating_img_url_small":"http://media3.ak.yelpcdn.com/static/201012163952475669/img/ico/stars/stars_small_3_half.png"},{"distance":9908.2171760936271,"mobile_url":"http://m.yelp.com/biz/UFpVz4UZjk_snJ1aES9rwg","rating_img_url":"http://media3.ak.yelpcdn.com/static/201012162578611207/img/ico/stars/stars_5.png","review_count":9,"name":"Toro Bravo Mexican Restaurant","rating":5.0,"url":"http://www.yelp.com/biz/toro-bravo-mexican-restaurant-gulf-breeze","location":{"city":"Gulf Breeze","display_address":["3755 Gulf Breeze Pkwy","Ste I","Gulf Breeze, FL 32563"],"geo_accuracy":8,"postal_code":"32563","country_code":"US","address":["3755 Gulf Breeze Pkwy","Ste I"],"coordinate":{"latitude":30.389774299999999,"longitude":-87.066389999999998},"state_code":"FL"},"phone":"8509161221","snippet_text":"Delicious authentic Mexican at great prices.  Get the chicken fajitas on Thursday night for only $6.99 or margaritas on Tuesday nights for only 99 cents....","image_url":"http://s3-media2.ak.yelpcdn.com/bphoto/7-UzxyvpWcqeIdZj66oy5Q/ms.jpg","snippet_image_url":"http://s3-media3.ak.yelpcdn.com/photo/VlWKsIWVCRQ-jEiqpMpeBA/ms.jpg","display_phone":"+1-850-916-1221","rating_img_url_large":"http://media1.ak.yelpcdn.com/static/20101216354709277/img/ico/stars/stars_large_5.png","id":"toro-bravo-mexican-restaurant-gulf-breeze","categories":[["Mexican","mexican"]],"rating_img_url_small":"http://media3.ak.yelpcdn.com/static/201012161949604803/img/ico/stars/stars_small_5.png"},{"distance":21264.025574983993,"mobile_url":"http://m.yelp.com/biz/fIvHhoeOlqlM93EFh8lmpQ","rating_img_url":"http://media2.ak.yelpcdn.com/static/201012164084228337/img/ico/stars/stars_4.png","review_count":9,"name":"Vallarta Mexican Restaurant","rating":4.0,"url":"http://www.yelp.com/biz/vallarta-mexican-restaurant-pensacola","location":{"city":"Pensacola","display_address":["8971 Pensacola Blvd","Pensacola, FL 32534"],"geo_accuracy":8,"postal_code":"32534","country_code":"US","address":["8971 Pensacola Blvd"],"coordinate":{"latitude":30.527099609375,"longitude":-87.275199890136705},"state_code":"FL"},"phone":"8504765262","snippet_text":"Pensacola Popular! =D cheese dip = Heaven","image_url":"http://s3-media4.ak.yelpcdn.com/bphoto/7K-jGtsxZTOjKuM19pigGw/ms.jpg","snippet_image_url":"http://media3.ak.yelpcdn.com/static/201012161986305257/img/gfx/blank_user_medium.gif","display_phone":"+1-850-476-5262","rating_img_url_large":"http://media4.ak.yelpcdn.com/static/20101216169592178/img/ico/stars/stars_large_4.png","id":"vallarta-mexican-restaurant-pensacola","categories":[["Mexican","mexican"]],"rating_img_url_small":"http://media2.ak.yelpcdn.com/static/20101216418129184/img/ico/stars/stars_small_4.png"},{"distance":17372.878836319251,"mobile_url":"http://m.yelp.com/biz/abTSgQL_oRLvyBrTYrnOfA","rating_img_url":"http://media3.ak.yelpcdn.com/static/201012161235323114/img/ico/stars/stars_3_half.png","review_count":7,"name":"La Hacienda Mexican Restaurant","rating":3.5,"url":"http://www.yelp.com/biz/la-hacienda-mexican-restaurant-pensacola-2","location":{"city":"Pensacola","display_address":["8084 N Davis Hwy","Pensacola, FL 32514"],"geo_accuracy":8,"postal_code":"32514","country_code":"US","address":["8084 N Davis Hwy"],"coordinate":{"latitude":30.5098991394043,"longitude":-87.220901489257798},"state_code":"FL"},"phone":"8504942130","snippet_text":"the service at la hacienda is top notch. you're sat as soon as you come in, chips and salsa as soon as you sit, server as soon as you get your chips and...","image_url":"http://s3-media4.ak.yelpcdn.com/bphoto/bNTMjJgUstwgBhmaxagCeQ/ms.jpg","snippet_image_url":"http://s3-media4.ak.yelpcdn.com/photo/x15GSPCBMRoPvVOmPvw-AA/ms.jpg","display_phone":"+1-850-494-2130","rating_img_url_large":"http://media1.ak.yelpcdn.com/static/201012161161255655/img/ico/stars/stars_large_3_half.png","id":"la-hacienda-mexican-restaurant-pensacola-2","categories":[["Mexican","mexican"]],"rating_img_url_small":"http://media3.ak.yelpcdn.com/static/201012163952475669/img/ico/stars/stars_small_3_half.png"},{"distance":14224.929871172077,"mobile_url":"http://m.yelp.com/biz/ChwvahuBERsYdrwlU2MWRg","rating_img_url":"http://media4.ak.yelpcdn.com/static/201012163106483837/img/ico/stars/stars_4_half.png","review_count":7,"name":"Taco Rock","rating":4.5,"url":"http://www.yelp.com/biz/taco-rock-pensacola","location":{"city":"Pensacola","display_address":["5454 Pensacola Blvd","Pensacola, FL 32505"],"geo_accuracy":8,"postal_code":"32505","country_code":"US","address":["5454 Pensacola Blvd"],"coordinate":{"latitude":30.469600677490199,"longitude":-87.243598937988295},"state_code":"FL"},"phone":"8504362080","snippet_text":"yes, Yes, YES!!!  Real Mexican food in Pensacola.  \n\nI have been visiting here from California and have been dying for real Mexican food.  Some of...","image_url":"http://s3-media4.ak.yelpcdn.com/bphoto/xu52oukDlihjAwj1yMj_JA/ms.jpg","snippet_image_url":"http://s3-media4.ak.yelpcdn.com/photo/J8tjmmtWHonDkM_o8Ar7TA/ms.jpg","display_phone":"+1-850-436-2080","rating_img_url_large":"http://media2.ak.yelpcdn.com/static/201012162752244354/img/ico/stars/stars_large_4_half.png","id":"taco-rock-pensacola","categories":[["Restaurants","restaurants"]],"rating_img_url_small":"http://media4.ak.yelpcdn.com/static/201012161127761206/img/ico/stars/stars_small_4_half.png"},{"distance":10525.534364389803,"mobile_url":"http://m.yelp.com/biz/o58NSWL6t3Ne-5B8NZ55RA","rating_img_url":"http://media1.ak.yelpcdn.com/static/201012161694360749/img/ico/stars/stars_3.png","review_count":13,"name":"Cactus Flower Cafe","rating":3.0,"url":"http://www.yelp.com/biz/cactus-flower-cafe-pensacola","location":{"city":"Pensacola","display_address":["3425 N 12th Ave","Pensacola, FL 32503"],"geo_accuracy":8,"postal_code":"32503","country_code":"US","address":["3425 N 12th Ave"],"coordinate":{"latitude":30.448200225830099,"longitude":-87.208297729492202},"state_code":"FL"},"phone":"8504328100","snippet_text":"Reasons to go:\n\n1. Amazing Guacomole\n2. Fresh Homemade Tortillas\n3. Sante Fe Tacos are amazing\n4. Killer Queso\n5. Fast and Friendly Service\n6. Everything...","image_url":"http://s3-media4.ak.yelpcdn.com/bphoto/KT5oLq46QLJ_ZNcEw5r3sw/ms.jpg","snippet_image_url":"http://s3-media1.ak.yelpcdn.com/photo/87kgcPLESplUSBLarHeiTA/ms.jpg","display_phone":"+1-850-432-8100","rating_img_url_large":"http://media3.ak.yelpcdn.com/static/201012161053250406/img/ico/stars/stars_large_3.png","id":"cactus-flower-cafe-pensacola","categories":[["Mexican","mexican"]],"rating_img_url_small":"http://media1.ak.yelpcdn.com/static/201012162337205794/img/ico/stars/stars_small_3.png"},{"distance":16155.774787082219,"mobile_url":"http://m.yelp.com/biz/gt1CSI-85MEuhk6BtU3Agw","rating_img_url":"http://media1.ak.yelpcdn.com/static/201012161694360749/img/ico/stars/stars_3.png","review_count":3,"name":"Cazadores Mexican Restaurant","rating":3.0,"url":"http://www.yelp.com/biz/cazadores-mexican-restaurant-pensacola","location":{"city":"Pensacola","display_address":["8183 W Fairfield Dr","Pensacola, FL 32506"],"geo_accuracy":8,"postal_code":"32506","country_code":"US","address":["8183 W Fairfield Dr"],"coordinate":{"latitude":30.4221000671387,"longitude":-87.317398071289105},"state_code":"FL"},"phone":"8504574747","snippet_text":"This is a good choice if you are looking for a no frills good tasting Mexican dining experience. Being from California and eating Mexican food has been a...","snippet_image_url":"http://s3-media4.ak.yelpcdn.com/photo/dOEivD3bcXWlwW58Pje7Ug/ms.jpg","display_phone":"+1-850-457-4747","rating_img_url_large":"http://media3.ak.yelpcdn.com/static/201012161053250406/img/ico/stars/stars_large_3.png","id":"cazadores-mexican-restaurant-pensacola","categories":[["Mexican","mexican"]],"rating_img_url_small":"http://media1.ak.yelpcdn.com/static/201012162337205794/img/ico/stars/stars_small_3.png"},{"distance":11873.25940529177,"mobile_url":"http://m.yelp.com/biz/M2p1sXdhNdXgTWx-aZbDyA","rating_img_url":"http://media3.ak.yelpcdn.com/static/201012162480304591/img/ico/stars/stars_1.png","review_count":1,"name":"Taco Bell","rating":1.0,"url":"http://www.yelp.com/biz/taco-bell-pensacola-3","phone":"8504329729","snippet_text":"It makes my day to drive by and see misspelled words on their magnetic outdoor signs.  If the lazy illiterate person would simply turn around and read what...","location":{"city":"Pensacola","display_address":["2111 W Fairfield Dr","Pensacola, FL 32505"],"geo_accuracy":8,"postal_code":"32505","country_code":"US","address":["2111 W Fairfield Dr"],"coordinate":{"latitude":30.4451007843018,"longitude":-87.241302490234403},"state_code":"FL"},"display_phone":"+1-850-432-9729","rating_img_url_large":"http://media1.ak.yelpcdn.com/static/201012163480014692/img/ico/stars/stars_large_1.png","id":"taco-bell-pensacola-3","snippet_image_url":"http://s3-media1.ak.yelpcdn.com/photo/BqKpWdIANkWdy5VoXya4Mg/ms.jpg","rating_img_url_small":"http://media3.ak.yelpcdn.com/static/20101216926385439/img/ico/stars/stars_small_1.png"}]}

26 thoughts on “Yelp API Example

  1. I create a leave a response when I especially enjoy a post on a website or I have
    something to add to the discussion. Usually it’s caused by the
    passion communicated in the post I looked at. And on this post Yelp API Example
    | Thys Michels Blog. I was moved enough to drop a comment
    😉 I actually do have a couple of questions for you if you don’t mind.
    Is it only me or do some of these comments look like written by brain dead people?
    😛 And, if you are writing on other sites, I would like to
    follow everything fresh you have to post.
    Could you list all of all your shared sites like your Facebook page, twitter feed, or linkedin profile?

  2. Iཿm not sure where you are getting your info, but good topic.

    I needs to spend some time learning more or understanding
    more. Thanks for great info I was looking for this info for my mission.

  3. Howdy! I could have sworn I’ve been to this website before but after looking at some of the
    articles I realized it’s new to me. Anyhow, I’m definitely happy I discovered it and I’ll
    be bookmarking it and checking back frequently!

  4. I loved as much as you’ll receive carried out right here.
    The sketch is tasteful, your authored subject matter stylish.
    nonetheless, you command get got an nervousness
    over that you wish be delivering the following.

    unwell unquestionably come more formerly again as
    exactly the same nearly very often inside case you shield this increase.

  5. Hi, Neat post. There is an issue along with your web
    site in internet explorer, could test this? IE nonetheless is the marketplace chief and a big section of other folks will omit
    your magnificent writing due to this problem.

  6. Admiring the commitment you put into your website and detailed information
    you offer. It’s good to come across a blog every once in a while
    that isn’t the same unwanted rehashed information.
    Fantastic read! I’ve saved your site and I’m including your RSS feeds to my Google account.

  7. We’re a group of volunteers and opening a new scheme in our
    community. Your website provided us with valuable info to work
    on. You have done a formidable job and our whole community will be grateful to you.

  8. My partner and I stumbled over here by a different web
    page and thought I might as well check things out. I like what
    I see so now i am following you. Look forward to finding out
    about your web page again.

  9. I got this website from my buddy who informed me on the topic of this web site
    and now this time I am browsing this web page and reading very informative posts at this time.

  10. Wonderful blog you have here but I was wondering if you knew
    of any forums that cover the same topics talked about here?
    I’d really love to be a part of community where I can get suggestions
    from other experienced people that share the same
    interest. If you have any suggestions, please let me know.
    Bless you!

  11. Generally I do not learn article on blogs, but I would like to say that this write-up
    very compelled me to try and do it! Your writing style has been surprised me.
    Thank you, very nice post.

  12. I loved as uch as you’ll receive carried out right here.
    The sketch is tasteful, your authored material stylish.

    nonetheless, you command get got an impatience over that
    you wish be delivering the following. unwell unquestionably come more formerly again since exactly tthe same
    nearly very often inside case you sheld this increase.

  13. Here are a few select apps that can help you with motivation, organization, and ultimately
    just help you get things done. Aikido focuses on realizing synchronization between ‘ki’ (spirit) and ‘tai’ (the body).

    ” As a result of the fact that physical education programs are being cut, the U.

  14. Hey There. I found your blog the use of msn. This is
    an extremely well written article. I will make sure to bookmark it and return to read
    more of your helpful information. Thanks
    for the post. I’ll certainly return.

  15. hey there and thank you for your info – I have definitely picked
    up anything new from right here. I did however
    expertise some technical issues using this website, as I experienced to reload
    the website lots of times previous to I could get it to load correctly.
    I had been wondering if your web hosting is OK? Not that I’m complaining,
    but slow loading instances times will often affect your placement in google and can damage your high quality score
    if advertising and marketing with Adwords. Well I’m adding this RSS to my e-mail and can look out for
    much more of your respective intriguing content. Make sure you
    update this again soon.

  16. I am really enjoying the theme/design of your blog. Do you ever run into any internet browser compatibility issues?
    A small number of my blog readers have complained about my blog
    not operating correctly in Explorer but looks great in Opera.
    Do you have any tips to help fix this issue?

  17. If your functions in YelpApi2 class which extends DefaultApi10a only return null?
    I tried to implement the same but getting error: OAuthException Error: Error While Creating Api object.
    Please explain.
    My code below:
    class TwoStepOAuth extends DefaultApi10a {

    @Override
    public String getAccessTokenEndpoint() {
    return null;
    }

    @Override
    public String getAuthorizationUrl(Token arg0) {
    return null;
    }

    @Override
    public String getRequestTokenEndpoint() {
    return null;
    }
    }

    In other class, I am trying below:
    OAuthService service=new ServiceBuilder().provider(TwoStepOAuth.class).apiKey(yelpConsumerKey).apiSecret(yelpConsumerSecret).build();
    Token accessToken=new Token(yelpToken, yelpTokenSecret);

  18. I wonder if this brokers demanding adverse balances have an understanding of what a
    damage they do to their small business. What are they hoping for?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s