I’m trying to make a tweet search using keywords and a bounding box so I can get what people from a certain part of the world are tweeting about. But my query is not working.
This is the endpoint I’m using, I curling from R, but any help is welcomed:
<code>curl --request GET 'https://api.twitter.com/2/tweets/search/recent?
'query=(<WORD1>%20OR%20<WORD2>%20OR%20<WORD3>)%20lang:es&expansions=geo.place_id&place.fields="geo"{"type":"Feature","bbox":[-100.3509,25.7045,-100.2675,25.666],"properties":{}}
</code>
<code>curl --request GET 'https://api.twitter.com/2/tweets/search/recent?
'query=(<WORD1>%20OR%20<WORD2>%20OR%20<WORD3>)%20lang:es&expansions=geo.place_id&place.fields="geo"{"type":"Feature","bbox":[-100.3509,25.7045,-100.2675,25.666],"properties":{}}
</code>
curl --request GET 'https://api.twitter.com/2/tweets/search/recent?
'query=(<WORD1>%20OR%20<WORD2>%20OR%20<WORD3>)%20lang:es&expansions=geo.place_id&place.fields="geo"{"type":"Feature","bbox":[-100.3509,25.7045,-100.2675,25.666],"properties":{}}
And in R:
<code>library(jsonlite)
library(httr)
library(tidyverse)
headers <- c(`Authorization` = sprintf('Bearer %s', bearer_token))
bbox <- c(-100.35094926882688,25.70447111708052,-100.26752438010853,25.665950026981893 )
keywords <- c("(<WORD1> OR <WORD2> OR <WORD3>)")
keywords <- paste0(keywords," lang:es")
endpoint <- "https://api.twitter.com/2/tweets/search/recent"
query <- paste(URLencode(keywords),
sep=""
)
geo_json <- list(
type = "Feature",
bbox = bbox,
properties = NULL
)
geo_json_str <- toJSON(geo_json, auto_unbox = TRUE)
geo_code <- paste("geo.place_id&",
"place.fields=",
'"geo": ',geo_json_str,sep = "")
response <- GET(
url = endpoint,
add_headers(.headers = headers),
query = list(query = query,
expansions=geo_code)
)
# Check the status of the response
if (status_code(response) != 200) {
stop("Failed to fetch tweets: ", status_code(response))
}
</code>
<code>library(jsonlite)
library(httr)
library(tidyverse)
headers <- c(`Authorization` = sprintf('Bearer %s', bearer_token))
bbox <- c(-100.35094926882688,25.70447111708052,-100.26752438010853,25.665950026981893 )
keywords <- c("(<WORD1> OR <WORD2> OR <WORD3>)")
keywords <- paste0(keywords," lang:es")
endpoint <- "https://api.twitter.com/2/tweets/search/recent"
query <- paste(URLencode(keywords),
sep=""
)
geo_json <- list(
type = "Feature",
bbox = bbox,
properties = NULL
)
geo_json_str <- toJSON(geo_json, auto_unbox = TRUE)
geo_code <- paste("geo.place_id&",
"place.fields=",
'"geo": ',geo_json_str,sep = "")
response <- GET(
url = endpoint,
add_headers(.headers = headers),
query = list(query = query,
expansions=geo_code)
)
# Check the status of the response
if (status_code(response) != 200) {
stop("Failed to fetch tweets: ", status_code(response))
}
</code>
library(jsonlite)
library(httr)
library(tidyverse)
headers <- c(`Authorization` = sprintf('Bearer %s', bearer_token))
bbox <- c(-100.35094926882688,25.70447111708052,-100.26752438010853,25.665950026981893 )
keywords <- c("(<WORD1> OR <WORD2> OR <WORD3>)")
keywords <- paste0(keywords," lang:es")
endpoint <- "https://api.twitter.com/2/tweets/search/recent"
query <- paste(URLencode(keywords),
sep=""
)
geo_json <- list(
type = "Feature",
bbox = bbox,
properties = NULL
)
geo_json_str <- toJSON(geo_json, auto_unbox = TRUE)
geo_code <- paste("geo.place_id&",
"place.fields=",
'"geo": ',geo_json_str,sep = "")
response <- GET(
url = endpoint,
add_headers(.headers = headers),
query = list(query = query,
expansions=geo_code)
)
# Check the status of the response
if (status_code(response) != 200) {
stop("Failed to fetch tweets: ", status_code(response))
}
New contributor
Gerardo Mora M. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.