我遵循几年前在堆栈溢出线程上提供的代码,但是当我运行"fromJSON"命令"代码 86,此方法需要 GET 或 HEAD"时出现错误。我正在尝试从公共Twitter列表中获取用户名列表(所有NHL球员(。
这是代码
# load packages
library("twitteR")
library("rjson")
library("httr")
consumer_key <- "XXX"
consumer_secret <- "XXX"
access_token <- "XXX"
access_secret <- "XXX"
options(httr_oauth_cache=T)
setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
twlist <- "nhl-players"
twowner <- "NHL"
api.url <- paste0("https://api.twitter.com/1.1/lists/members.json?slug=",
twlist, "&owner_screen_name=", twowner, "&count=500")
response <- POST(api.url, config(token=twitteR:::get_oauth_sig()))
fromJSON(content(response, as = "text", encoding = "UTF-8"))
我想我已经明白了。您的api.url
对象中有一点错别字。它不是"&NHL="(这是实际值(,而是"&owner_screen_name="(这是您要传递的参数的名称(。另外,您需要使用 GET 请求。
api.url <- paste0("https://api.twitter.com/1.1/lists/members.json?slug=", twlist, "&owner_screen_name=", twowner, "&count=500")
response <- GET(api.url, config(token=twitteR:::get_oauth_sig()))
对于像我这样挣扎的人。这是现在有效的代码:
api.url <- paste0("https://api.twitter.com/1.1/lists/members.json?slug=", twlist, "&owner_screen_name=", twowner, "&cursor=-1&count=1000")
response <- GET(api.url, config(token = twitteR:::get_oauth_sig()))
如您所见,您需要添加&cursor=-1