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"}]}
0.000000
0.000000
Like this:
Like Loading...