问题
Trying to connect to the yelp API using R and library ROAuth.
Great python sample using the rauth module and geo-coordinates:
https://gist.github.com/phillipjohnson/8889618
and would like to do exactly that but in R and using a library like ROAuth.
I've been attempting to create handshakes, etc:
credentials <- OAuthFactory$new(consumerKey=consumerKey,
consumerSecret=consumerSecret,
oauthKey = token,
oauthSecret = token_secret,
authURL="http://api.yelp.com/v2")
credentials$handshake()
credentials$OAuthRequest(testURL, "GET")
But not getting past the handshake. Yelp uses OAuth 1.0 which ROAuth package supports. From other code I've seen it requires an 'oauth_consumer_key', 'oauth_nonce', 'oauth_signature_method', 'oauth_timestamp', 'oauth_token'. I would appreciate any tips from people who used R to query Yelp using geo-coordinates. Thanks!
回答1:
After the author of ROAuth recommended using library(httr) and because of the lack of simple yelp examples in R using either libraries, I figured others may be looking for this too. This will return either 10 bars in the Chicago area by name, or 10 bars in San Francisco by geo-coordinates. Replace the x's with your own yelp account keys. (this is pieced together from many sources- thanks to all of them).
# yelp
consumerKey = "xxxx"
consumerSecret = "xxxx"
token = "xxxx"
token_secret = "xxxx"
require(httr)
require(httpuv)
require(jsonlite)
# authorization
myapp = oauth_app("YELP", key=consumerKey, secret=consumerSecret)
sig=sign_oauth1.0(myapp, token=token,token_secret=token_secret)
limit <- 10
# 10 bars in Chicago
yelpurl <- paste0("http://api.yelp.com/v2/search/?limit=",limit,"&location=Chicago%20IL&term=bar")
# or 10 bars by geo-coordinates
yelpurl <- paste0("http://api.yelp.com/v2/search/?limit=",limit,"&ll=37.788022,-122.399797&term=bar")
locationdata=GET(yelpurl, sig)
locationdataContent = content(locationdata)
locationdataList=jsonlite::fromJSON(toJSON(locationdataContent))
head(data.frame(locationdataList))
来源:https://stackoverflow.com/questions/27728318/using-the-yelp-api-with-r-attempting-to-search-business-types-using-geo-coordin