Search code examples
rtwitteroauthtwitter-oauthrcurl

How to send tweets in R?


Possibly a duplicate question with Using R to send tweets but unfortunatly it provides only answers on how to use OAuth for R.

I have installed the TwitteR package from Jeff Gentry and I'm able to authenticate myself using Oauth, but from what I see in the documentation there are functions to manipulate Twitter direct messages (dmSend and dmGet ) but I cannot find anything to send tweets.

  > consumer_key <- "xxx"
  > consumer_secret <- "zzz"
  > access_token <- "aaa"
  > access_secret <- "bbb"
  > setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
  [1] "Using direct authentication"

Now that I'm authenticate, how can I use RCurl to submit a valid HTML form and send tweets ?

Thank you,


Solution

  • In the twitteR package:

    updateStatus("Tweet goes here.")
    

    Alternate httr way:

    # assign keys and secrets
    api_key <- "a"
    api_secret <- "b"
    access_token <- "c"
    access_token_secret <- "d"
    
    # authenticate with httr
    oauth_endpoints("twitter")
    myapp <- oauth_app("twitter", key = api_key, secret = api_secret)
    twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)
    
    tweet <- "Tweet goes here."
    tweet <- gsub(" ","%20", tweet)
    
    httr::POST(
      url = paste0("https://api.twitter.com/1.1/statuses/update.json?status=", tweet, "&display_coordinates=false"),
      config(token = twitter_token)
    )