ropensci-archive / rtweet

🐦 R client for interacting with Twitter's [stream and REST] APIs
https://docs.ropensci.org/rtweet
Other
786 stars 201 forks source link

Bearer Token RTWEET #755

Closed jimsharma206 closed 1 year ago

jimsharma206 commented 1 year ago

Problem

I am looking to stream tweets via the rtweet app. I have all the tokens necessary, including the bearer token. However, I do not know how to insert the bearer token in my code so that it gets authenticated.

Expected behavior

Reproduce the problem

## insert code here

api_key <- "******************"
api_key_secret <- "**********************"
access_token <- "******************"
access_token_secret <- "******************"

#authenticate via web browser

token <- create_token(
   app= "Demo app v1",
   consumer_key = api_key,
   consumer_secret=api_key_secret,
   access_token=access_token,
   access_secret=access_token_secret
 )

bearer_token<-"MY BEARER TOKEN "
app <- rtweet_app(bearer_token = Sys.getenv("RTWEET_BEARER"))
auth_as(app)

rtweet_app(bearer_token)

#check_token_v2("************************************88x")

vacc_tweets <- search_tweets("#emilyinparis", n=5000, include_rts = FALSE, lang="en")
corp_stream <- stream_tweets("#Covid", timeout = 30) %>%
  corpus(docid_field="id_str")

corp_stream <- filtered_stream("#Covid", timeout = 30, parse=FALSE) %>%
  corpus(docid_field="id_str")

rtweet version

## copy/paste output
packageVersion("rtweet")

Session info

## copy/paste output
sessionInfo()
llrs commented 1 year ago

Where does the corpus function come from? The check_token_v2 function is not exported, please do not use it. You don't provide rules for the filtered_streamendpoint. Please check the documentation, read it carefully and test what works before opening an issue.

To insert a token you would only need to have ..., token = app) or simply use auth_as(app) and it should be picked up automatically.

llrs commented 1 year ago

What is the output of is(auth_get())? If it is not rtweet_bearer then you have the incorrect authentication. If it is and fails, or there are crashes, please paste the error if possible. If it is slow and it takes much longer than the timeout this is know and will improve in future releases (hopefully).

I recommend to use a valid filename, having something with a # symbol might not be valid and may cause problems in your OS.

I investigated a bit more, you seem to be using corpus from the quanteda package. But doesn't seem to be related to your problem with the package.

jimsharma206 commented 1 year ago

I am sorry to bother you sir, but I simply do not know how to proceed. I entered my bearer token after running the rtweet_app() function. However, when I run stream_bitcoin below, I get the following error: ! A bearer token is needed for this endpoint.

library(rtweet)

rtweet_app()

stream_bitcoin <- filtered_stream(query="#bitcoin", timeout = 30, parse = FALSE)

llrs commented 1 year ago

You need to say rtweet which token you want to use. See the two lines I added to your example:

library(rtweet)

token <- rtweet_app() # Modified line
auth_as(token) # Added line
stream_bitcoin <- filtered_stream(query="#bitcoin", timeout = 30, parse = FALSE)

This is explained in the Default authentication section of one of the vignettes.

I also recomend you to save your token via

auth_save(token, "my_app")

So next time you won't need to use rtweet_app but simply load the token via auth_as