skip to Main Content

With little experience in API programming, I am looking for a way to send a DELETE or POST request using the Twitter API v2.0 with the httr package in R. I usually use the academictwitteR package to interact with the API, but the package uses a bearer token authentication that seems to me to not be adequate for write privileges (required by DELETE and POST). Therefore, it seemed that the first step is setting up the OAuth 1.0a authentication credintials as described here. I downloaded and stored the four variables (oauth_token; oauth_token_secret; oauth_consumer_key; oauth_consumer_secret) from the app I created, but then I am stuck as to how to set up the request with httr.

I can’t provide an example since I could not figure out the code, I hope you understand. Any help is much appreciated!

2

Answers


  1. I looked at the curl examples for DELETE and POST requests you provided. They are rewrotten in R below. You need to replace the $OAUTH_SIGNATURE though. Please let me know if it works for you as I cannot check the code as I do not have a Twitter account nor do I have the OAuth token.

    library(httr)
    
    r <- DELETE(url = "https://api.twitter.com/2/users/2244994945/following/6253282",
                add_headers('Authorization'= 'OAuth $OAUTH_SIGNATURE'))
    
    content(r)
    
    
    r <- POST(
      url = "https://api.twitter.com/2/users/6253282/following",
      body = c("target_user_id" = "2244994945"),
      add_headers('Authorization'= 'OAuth $OAUTH_SIGNATURE'),
      content_type_json()
      )
    
    content(r)
    
    Login or Signup to reply.
  2. Not sure about your specific requirements and what you tried before.

    Would it be a solution to just use the rtweet package?

    It is quite handy and offers quite a lot of functions to interact with the twitter api(also posting and deleting tweets possible)

    E.g.

    #Posting
    post_tweet("hello world")
    
    #Following
    post_follow("someuser")
    
    # Not sure about deleting, but should work like this
    post_tweet(destroy_id= "postID")
    

    To get you post ID to delete it, you can get maybe use get_my_timeline. This should give your posts with ids.

    See here in the vignette for a short intro about functions.

    Of course you also need an access token first.
    They have a very good explanation page on how to do this. Also a FAQ for problems. The explanation is rather long and probably too specific to go through in detail here. But would be interesting to know if it works for you.

    Further things:

    1. Make sure you have httpuv installed
    2. Be sure to have the very latest rtweet version (best is from github I think)
    3. Also check the following: Go to developer.twitter.com/en/apps and then to your app. Under ‘Permissions’ make sure to give Read and Write. Under ‘App Detail’ add 127.0.0.1:1410 as Callback Url. Under ‘Keys and tokens’ create your Keys
    4. Regenerate your tokens/keys

    So try this:

    install.packages("httpuv")
    devtools::install_github("mkearney/rtweet")
    
    library(rtweet)
    library(httpuv)
    
    create_token(
    app = appname,
    consumer_key = key,
    consumer_secret = secret,
    access_token = access_token,
    access_secret = access_secret
    )
    

    Update
    I just saw, on the very latest version in github they completely changed their methods and create_token is now depreciated.

    Here is the new way to do it: documentation

    So seems you habe to use rtweet_bot() now.

    library("askpass")
    rtweet_bot(
      api_key = askpass("API key"),
      api_secret = askpass("API secret"),
      access_token = askpass("access token"),
      access_secret = askpass("access token")
    )
    

    The rest of the code I posted should stay the same.

    In general it seems there are 3 ways of authenticating now:

    rtweet_user() interactively authenticates an existing twitter user.
    This form is most appropriate if you want rtweet to control your
    twitter account.

    rtweet_app() authenticates as a twitter application. An application
    can’t perform actions (i.e. it can’t tweet) but otherwise has
    generally higher rate limits (i.e. you can do more searches). See
    details at
    https://developer.twitter.com/en/docs/basics/rate-limits.html. This
    form is most appropriate if you are collecting data.

    rtweet_bot() authenticates as bot that takes actions on behalf of an
    app. This form is most appropriate if you want to create a twitter
    account that is run by a computer, rather than a human.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search