Using the Yelp API with R, attempting to search business types using geo-coordinates

早过忘川 提交于 2019-11-29 10:29:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!