I am a beginner using the API. Need to connect with R, how can I start?

I haven’t seen any examples of V2 APIs in R, but you may be able to piece some together yourself using httr directly: httr/oauth1-twitter.r at main · r-lib/httr · GitHub and setting the appropriate parameters for the v2 endpoints - which is a bit involved.

If possible, Python or Java or Ruby or Javascript is a better choice GitHub - twitterdev/Twitter-API-v2-sample-code: Sample code for the Twitter API v2 endpoints

2 Likes

Hi @Caro_Escarlata

I’ve been using R with v2 of the Twitter API. I can send over a few examples, which endpoints are you looking to connect to?

1 Like

Hi, Jessica, thanks so much for your reply! I want to analyze tweets using R, but when I try to authenticate my credentials in R I have an error message. I need to download tweets from February - October 2016 on a specific topic, I have the keywords.

Sorry, I am completely new on this. :slight_smile:

This is the error I see:

Error in function type, msg, asError = TRUE

Unknown SSL protocol error in connection to api. twitter. com:443

Thanks! Do you have a code sample you can send over?

Thanks for reaching out here. I recommend you regenerate / revoke your keys and tokens and read more about our tips for keeping your credentials secure. Which endpoint are you trying to hit? Could you let us know more about the task you are working on?

Sure, I want to run the analysis is the Colombian peace agreement referendum (Plebiscito), celebrated on 2016-10-02 Some keywords are: #YoVotoNo, farc, paz, santos, colombia, #SoyColombiaNO. I need data from Feb to Oct 2016 and then, analyze the data using R. My topic of research is: weaponization on social media.

Ah, you might be able to use the sandbox version of the premium search for this task. I’ve been able to use Search-Tweets-Python in R for premium endpoints. Here is a blog post on this subject.

Use the rtweet package.

1 Like

Hi Jessica.
I was wondering if you could those examples of using R with v2 of the Twitter API to me as well please. I’ve been trying using httr but I just get back empty dataframes.

Thanks
Eugene

Which endpoint are you connecting to? Here is one sample using recent search:

require(httr)

bearer_token <- ""

headers <- c(`Authorization` = sprintf('Bearer %s', bearer_token))

params = list(
  `query` = 'TwitterDev',
  `max_results` = '10',
  `tweet.fields` = 'created_at,lang,conversation_id'
)


response <- httr::GET(url = 'https://api.twitter.com/2/tweets/search/recent', httr::add_headers(.headers=headers), query = params)
print(response)


obj <- httr::content(res, as = "text")
print(obj)
1 Like

Hi Jessica
Thanks for getting back to me.
When I run the code above, inserting my bearer token (from https://developer.twitter.com/en/portal/projects/1314487184142016513/apps/16111049/keys), I get an empty response which suggests I’m not authorised:

print(obj)
[1] "{\"title\":\"Unauthorized\",\"type\":\"about:blank\",\"status\":401,\"detail\":\"Unauthorized\"}"

Not sure what I did wrong.

Thanks
Eugene

Interesting. Have you been able to make a call using curl or another library or even a rest client such as Postman?

Hi Jessica. No I haven’t tried curl or Postman. I was basing my work on a presentation given by Suhem Parack at WhyR who proposed something similar to your code above.
Thanks, Eugene