150 thoughts on “Twitter Authentication with R

  1. Hi Julian,

    I have downloaded twitter data using R in .csv format. These are the fields :

    text favorited favoriteCount replyToSN created truncated replyToSID id replyToUID statusSource screenName retweetCount isRetweet retweeted longitude latitude

    For a given tweet in .csv file, the value of retweetCount>0 though the value for retweeted=0. I am not able to exactly discern the meaning for these three different fields (retweetCount, isRetweet, retweeted).

    Could you please help me understand the same?

    TIA

    • Hey Nikhil,
      So
      retweetCount – How often it was retweeted
      isRetweet – Is this tweet a retweet or an original tweet?
      retweeted – Was the tweet retweeted? (true/false)

      So there seems to be a problem with the API. I don´t really know why it returns these values but I found a lot of similar questions in the Twitter forum but no real answer.
      I will take a look at this topic again.

      Regards

  2. Hi julian
    I am calculating sentiment from twitter tweets.I have a question can we fetch conversations from a single tweet that is fetched in search twitter function.

    • Hey Onkar,
      I think that this is not possible on the direct way. You can just search all the tweets you downloaded for connections. To have access to the whole Twitter data you would need something like a Firehose access.

      Regards

  3. I get the following error by executing : twitCred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”)):
    Error in function (type, msg, asError = TRUE) :
    Could not resolve host: api.twitter.com; Host not found, try again

    how to resolve this issue?

  4. Pingback: Analysis of devices used to post on Twitter – R code | kapoorabhishek

  5. Well after I make the authentication and all the necessary, I get this as a result!

    twitterMap(“socialmedia2day”, fileName=”Map_socialmedia2day.pdf”, nMax=3000)
    Getting data from Twitter, this may take a moment.
    Error in twInterfaceObj$doAPICall(paste(“users”, “show”, sep = “/”), params = params, :
    OAuth authentication is required with Twitter’s API v1.1

    Problem on twitter????

    • Hey Tasos,
      it seems like you forgot the last row in the code.
      The authentication is not done after you inserted the PIN. You have to execute the line:
      registerTwitterOAuth(twitCred)
      This line will then, if everything works well, return a “TRUE”.
      Then you are ready to go.

      Regards

  6. Hey Julian!

    Everything works until the point:

    > twitCred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”))
    Error: Unauthorized
    >
    > registerTwitterOAuth(twitCred)
    Error in registerTwitterOAuth(twitCred) :
    oauth has not completed its handshake

    Any idea?

      • Yep, did it line by line, but after running line 14, console says “Error: Unauthorized” :/ The keys are definetely correct (Used Mozilla and my personal Account)

      • Thanks again for your quick response! Somehow i got it yesterday until that point. I think the problem was a double-twitter-login with two different accounts…

      • same problem, but I use just one twitter account

        R logs

        > library(twitteR,igraph)
        Le chargement a nécessité le package : ROAuth
        Le chargement a nécessité le package : RCurl
        Le chargement a nécessité le package : bitops
        Le chargement a nécessité le package : digest
        Le chargement a nécessité le package : rjson
        > library(RCurl)
        > options(RCurlOptions = list(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”)))
        > require(twitteR)
        > reqURL accessURL authURL apiKey apiSecret twitCred twitCred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”))
        Erreur : Unauthorized
        > twitCred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”))
        Erreur : Unauthorized

        can you help me please ?

  7. library(twitteR)
    library(stringr)
    library(RCurl)

    reqURL <-"https://api.twitter.com/oauth/request_token&quot;
    accessURL <- "http://api.twitter.com/oauth/access_token&quot;
    authURL <- "http://api.twitter.com/oauth/authorize&quot;
    consumerKey="xxxxxxxxxxx" % i have this key
    comsumerSecret="xxxxxxxxxx" % i have this key

    download.file(url="http://curl.haxx.se/ca/cacert.pem&quot;,destfile="cacert.pem") % for what??

    twitCred <- OAuthFactory$new(consumerKey=consumerKey,
    consumerSecret=consumerSecret,
    requestURL=reqURL,
    accessURL=accessURL,
    authURL=authURL)

    twitCred$handshake(cainfo="cacert.pem") # curl is a command line tool for transfering
    registerTwitterOAuth(twitCred)

    Hai julian,

    I wanna ask you several question.

    1. I try to run this code but still error because "unauthorized". i am working with mac os.
    How to solve it?

    2.This code for what?? should i download this file?
    download.file(url="http://curl.haxx.se/ca/cacert.pem&quot;,destfile="cacert.pem")

    3.it there have different setting for the cacert.pem???

  8. Pingback: Create Twitter Wordcloud with Sentiments | julianhi's Blog

  9. Please help!! I am having “unauthorized” when started searching. This problem has been several days. So I decided to create another application today. Could that be my application setting? Below is my R console output:

    > Cred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”))
    To enable the connection, please direct your web browser to:
    https://api.twitter.com/oauth/authorize?oauth_token=YZGu3jRN2ZxG9IwvhvEJMcR9oXTw2GcV8geGp49ME
    When complete, record the PIN given to you and provide it here: 8357550
    > registerTwitterOAuth(Cred)
    [1] TRUE
    > save(Cred, file=”twitter authentication rkresnadi1.Rdata”)
    > twitlst <- searchTwitter("sustainable development", n=1000, cainfo="cacert.pem")
    [1] "Unauthorized"
    Error in twInterfaceObj$doAPICall(cmd, params, "GET", …) :
    Error: Unauthorized

    • Hey Stu,
      your code seems to be ok. I saw that you changes the name of the Credentials Object to Cred, but that shouldn´t be a problem if you did this in your whole code. But you receive a “TRUE” which normally tells you that the authentication was successful. Could you also please post the first part of your code?
      Regards

  10. please why am i getting error: forbidden
    > library(twitteR)
    Loading required package: ROAuth
    Loading required package: RCurl
    Loading required package: bitops
    Loading required package: digest
    Loading required package: rjson
    Warning messages:
    1: package ‘twitteR’ was built under R version 3.0.2
    2: package ‘ROAuth’ was built under R version 3.0.2
    3: package ‘RCurl’ was built under R version 3.0.2
    4: package ‘digest’ was built under R version 3.0.2
    > library(plyr)

    Attaching package: ‘plyr’

    The following object is masked from ‘package:twitteR’:

    id

    Warning message:
    package ‘plyr’ was built under R version 3.0.2
    > library(stringr)
    Warning message:
    package ‘stringr’ was built under R version 3.0.2
    > library(ROAuth)
    > download.file(url=”http://curl.haxx.se/ca/cacert.pem”, destfile=”cacert.pem”)
    trying URL ‘http://curl.haxx.se/ca/cacert.pem’
    Content type ” length 250283 bytes (244 Kb)
    opened URL
    downloaded 244 Kb

    > requestURL accessURL = “http://api.twitter.com/oauth/access_token”
    > authURL consumerKey = “myKey”
    > consumerSecret = “mySecret”
    > Cred Cred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”) )
    Error: Forbidden

    • Hey satya
      The updated code is the code above. But the code used in the question before was an Old version.
      So just use the code from the Tutorial and everything should work.

      Regards

  11. Hi julianhi,

    I’m having the same problem retrieving tweets.

    > tweets = searchTwitter(“sustainable development”, n=200, cainfo=”cacert.pem”)
    [1] “Unauthorized”
    Error in twInterfaceObj$doAPICall(cmd, params, “GET”, …) :
    Error: Unauthorized

    Here is my code, I’m putting it in line by line. Using RStudio Version 0.98.501 on Mac.

    > library(RCurl)
    > options(RCurlOptions = list(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”)))
    > require(twitteR)
    > reqURL accessURL authURL apiKey apiSecret twitCred twitCred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”))
    To enable the connection, please direct your web browser to:
    https://api.twitter.com/oauth/authorize?oauth_token=KiysFMXkw1ggdHGviVv5U8Yef4ooZxw7AZ89L1LSmFM
    When complete, record the PIN given to you and provide it here: 1234567
    > registerTwitterOAuth(twitCred)
    [1] TRUE
    > tweets = searchTwitter(“sustainable development”, n=200, cainfo=”cacert.pem”)
    [1] “Unauthorized”
    Error in twInterfaceObj$doAPICall(cmd, params, “GET”, …) :
    Error: Unauthorized

    Please help!

    CMG

    • Hi J,

      here’s the complete code, I messed up the last post. Like I said, I’m using RSudio on Mac

      > library(RCurl)
      Loading required package: bitops
      > options(RCurlOptions = list(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”)))
      > require(twitteR)
      Loading required package: twitteR
      Loading required package: ROAuth
      Loading required package: digest
      Loading required package: rjson
      > reqURL accessURL authURL apiKey apiSecret twitCred twitCred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”))
      To enable the connection, please direct your web browser to:
      https://api.twitter.com/oauth/authorize?oauth_token=epAv0cZJWd7sRayMvKwDPVjQtPiVnmX1dK1AKEZjVg
      When complete, record the PIN given to you and provide it here: 1234567
      > registerTwitterOAuth(twitCred)
      [1] TRUE
      > tweets = searchTwitter(“MMR”, n=200, cainfo=”cacert.pem”)
      [1] “Unauthorized”
      Error in twInterfaceObj$doAPICall(cmd, params, “GET”, …) :
      Error: Unauthorized

      • Hey CMG,
        sorry for the late answer but I was very busy.
        I can´t find a bug in your code at the moment.
        But did you try do execute it in the normal R console?
        Sometimes the authentication does not really complete in RStudio.

        Regards

  12. Pingback: Mining some Twitter data | DH + Data Viz

  13. Hello, I am having also the error forbidden during the handshake:

    To enable the connection, please direct your web browser to:
    http://api.twitter.com/oauth/authorize?oauth_token=FWJevpPdDnZrbuqmmkyrQzcUZzRNR3we83LlJdAXnY
    When complete, record the PIN given to you and provide it here: 1817865
    Error: Forbidden

    And I tried many times…..

    here is my code:

    library(twitteR)
    library(RCurl)
    # Set SSL certs globally
    options(RCurlOptions = list(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”)))

    require(twitteR)
    reqURL <- "https://api.twitter.com/oauth/request_token&quot;
    accessURL <- "http://api.twitter.com/oauth/access_token&quot;
    authURL <- "http://api.twitter.com/oauth/authorize&quot;
    consumerKey <- "xpArd5lrnnjxxxxxxxxxxxxxxxxxx"
    consumerSecret <- "jFF4fG3wrhx7Ya8P8xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    twitCred <- OAuthFactory$new(consumerKey=consumerKey,
    consumerSecret=consumerSecret,
    requestURL=reqURL,
    accessURL=accessURL,
    authURL=authURL)

    twitCred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
    registerTwitterOAuth(twitCred)

    What did I miss ? Do you think SSL is not working properly ?

  14. Good afternoon, I’ve read through the comments and while I saw the same issue (Error: Unauthorized) I didn’t see a way to resolve it.

    > twitCred$handshake(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”))
    To enable the connection, please direct your web browser to:
    https://api.twitter.com/oauth/authorize?oauth_token=xyz
    When complete, record the PIN given to you and provide it here:

    You mention to use the most updated code but I think I am. Here’s what I’m using:

    library(RCurl)
    # Set SSL certs globally
    options(RCurlOptions = list(cainfo = system.file(“CurlSSL”, “cacert.pem”, package = “RCurl”)))

    require(twitteR)
    reqURL <- "https://api.twitter.com/oauth/request_token&quot;
    accessURL <- "https://api.twitter.com/oauth/access_token&quot;
    authURL <- "https://api.twitter.com/oauth/authorize&quot;
    apiKey <- "removed"
    apiSecret <- "removed"

    twitCred <- OAuthFactory$new(consumerKey=apiKey,consumerSecret=apiSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)

    twitCred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))

    registerTwitterOAuth(twitCred)

  15. Pingback: Five minutes of Twitter Data Tutorial | Austin+Wehrwein

  16. Pingback: Cluster your Twitter Data with R and k-means - ThinkToStart

  17. Pingback: Sentiment Analysis on Twitter with Datumbox API - ThinkToStart

  18. Hey, I am having this problem while I am trying to extract 1499 tweets with #flipkart….

    In doRppAPICall(“search/tweets”, n, params = params, retryOnRateLimit = retryOnRateLimit, :
    1499 tweets were requested but the API can only return 100

    • Hey abhi,
      the Twitter API can just return a certain amount of tweets for a given keyword. This is a restriction by twitter. So for you there is no way of getting more than 100 tweets at the moment for your given searchterm. If you want to get more over time you can use the streaming API and save the tweets directly when they are posted on twitter. But this is real time and it needs some time to collect tweets.

      Regards

  19. And julianhi, one more request….
    Is there any way to extract tweets between certain date range…please help #stuck

  20. Hi julianhi! I have a problem with this script, i wish that you can help me

    install.packages(“ROAuth”)
    install.packages(“twitteR”)#comandi riguardo a twitter
    install.packages(“wordcloud”)#per analizzare graficamente i testi
    install.packages(“tm”)#permette di lavorare su dati di testo
    install.packages(“stringr”)
    install.packages(“RCurl”)

    library(“ROAuth”)
    library(“twitteR”)
    library(“wordcloud”)
    library(“tm”)
    library(“stringr”)
    library(“RCurl”)

    require(twitteR)
    require(ROAuth)
    require(stringr)

    download.file(url=”http://curl.haxx.se/ca/cacert.pem”, destfile=”cacert.pem”)
    cred <- OAuthFactory$new(consumerKey='xxxx',
    consumerSecret='xxx',
    requestURL='https://api.twitter.com/oauth/request_token&#039;,
    accessURL='https://api.twitter.com/oauth/access_token&#039;,
    authURL='https://api.twitter.com/oauth/authorize&#039 ;)
    cred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
    save(cred, file="twitter authentication.Rdata")
    registerTwitterOAuth(cred)
    r_stats<- searchTwitter("#Rstats", n=1500, cainfo="cacert.pem")

    Finally I have this error:
    [1] "Unauthorized"
    Errore in twInterfaceObj$doAPICall(cmd, params, "GET", …) :
    Error: Unauthorized

    but when I do registerTwitterOAuth(cred) my output is TRUE then I think my autentication is ok. Can you help me?
    Thanks a lot
    Angela

    • Hey Angela,
      I updated the code a few days ago. Please refresh the site and you should see it ;)
      The auth process got a lot easier in the newest twitteR version.
      Does this work for you?
      Regards

  21. Perfect! thanks a lot! I see this blog and I found some errors.. but with your code is all ok.. Can I ask you some question? In my prior code there is a destfile called cancert.pem, (sorry for the question, I’m not very well in R and in english too) but what is its rule in the code? thanks :)

    • The cacert.pem file is a collection of trusted root certification authorities. But it is enough to know that it was necessary for the steps to create the oAuth connection. The old code had a lot of manual steps and in the new version of the twitteR package these steps were replace with the httr package. And so you just need the one line code from my tutorial for the authentication. All these steps you had to do before like downloading the cacert.pem file happen automatically.

      Regards

  22. Sorry, I have another problem :( in this code:

    #save text
    r_stats_text <- sapply(r_stats, function(x) x$getText())
    r_stats_text
    #create corpus
    r_stats_text_corpus <- Corpus(VectorSource(r_stats_text))

    #clean up
    r_stats_text_corpus <- tm_map(r_stats_text_corpus, tolower)
    r_stats_text_corpus <- tm_map(r_stats_text_corpus, removeNumbers)
    r_stats_text_corpus <- tm_map(r_stats_text_corpus, removePunctuation)
    r_stats_text_corpus <- tm_map(r_stats_text_corpus, removeWords, stopwords('english'))
    r_stats_text_corpus wordcloud(r_stats_text_corpus)
    Error: inherits(doc, “TextDocument”) is not TRUE

    can you help me?
    thanks a lot!

  23. Hi Julian,
    As for the searchTwitter command, i notice that you said due to the restriction, it can only return the recent ones. But what is ‘recent’? like 1 week? 2 week? 1 month? 3 months?

    and in the searchTwitter command, i can only search for one keyword at a time. is there any other method that I can get the results that contain several keywords? for example, i want to get the tweets contain keywords of ‘A’ or ‘B’ or ‘C’ (not AND).

    Best Regards

    • Hey,
      sorry for the late answer.
      Recent is not defined by twitter. It always depends on the amount of tweets which contain for example the hashtag you are searching for. You have to try it out yourself for the keyword you are searching.

      I´m not sure how you could solve your second problem. But why don´t you just do 3 separate searches and save them into a dataframe. Wouldn´t that be a solution?

      Regards

    • Hey,
      seems like you used some old code. I updated the tutorial some time ago. Please refresh the page and delete your cache and try using the new code.

      Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s