R과 X API v2 시작하기
httr 및 jsonlite를 사용하여 R에서 X API v2 user lookup 엔드포인트를 호출하는 튜토리얼. bearer 토큰 설정 및 R에서 JSON 응답 파싱을 다룹니다.
Documentation Index
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
httr 및 jsonlite를 사용하여 R에서 X API v2 user lookup 엔드포인트를 호출하는 튜토리얼. bearer 토큰 설정 및 R에서 JSON 응답 파싱을 다룹니다.
Sys.setenv(BEARER_TOKEN = "your-bearer-token")
install.packages("httr")
install.packages("jsonlite")
install.packages("dplyr")
require(httr)
require(jsonlite)
require(dplyr)
bearer_token <- Sys.getenv("$BEARER_TOKEN")
headers <- c(`Authorization` = sprintf('Bearer %s', bearer_token))
params <- list(`user.fields` = 'description', `expansions` = 'pinned_tweet_id')
handle <- readline('$USERNAME')
url_handle <- sprintf('https://api.x.com/2/users/by?usernames=%s', handle)
response <-
httr::GET(url = url_handle,
httr::add_headers(.headers = headers),
query = params)
obj <- httr::content(response, as = "text")
print(obj)
json_data <- fromJSON(obj, flatten = TRUE) %>% as.data.frame View(json_data)
final <-
sprintf(
"Handle: %s\nBio: %s\nPinned Post: %s",
json_data$data.username,
json_data$data.description,
json_data$includes.tweets.text
)
cat(final)