Automated httr authentication with twitteR , provide response to interactive prompt in “batch” mode

后端 未结 3 1245
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 17:19

I am using the R package twitteR to post items to Twitter. I put everything inside of a function and it works fine. However, I would like to run the function without being

相关标签:
3条回答
  • 2020-12-03 18:13

    This works perfectly fine.

    install.packages("twitteR", dependencies = T)
    install.packages(c('ROAuth','RCurl'))
    install.packages("httr")
    library(httr)
    require('ROAuth')
    require('RCurl')
    library(twitteR)
    
    
    
    reqURL <- "https://api.twitter.com/oauth/request_token"
    
    accessURL <- "https://api.twitter.com/oauth/access_token"
    
    authURL <- "https://api.twitter.com/oauth/authorize"
    
    consumerKey <- "XXXXXXXXXXXXX"
    
    consumerSecret <- "XXXXXXXXXXXXXXXXXXXXX"
    
    twitCred <-     OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)
    
    download.file(url="https://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
    
     twitCred$handshake(cainfo="cacert.pem")
    
      setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
    
    0 讨论(0)
  • 2020-12-03 18:17

    You can try setting the httr_oauth_cache option to TRUE:

    options(httr_oauth_cache=T)
    

    The twitteR package uses the httr package, on the Token manual page for that package they give tips about caching:

    OAuth tokens are cached on disk in a file called .httr-oauth 
    saved in the current working directory. Caching is enabled if:
    
    The session is interactive, and the user agrees to it, OR
    
    The .httr-oauth file is already present, OR
    
    getOption("httr_oauth_cache") is TRUE
    
    You can suppress caching by setting the httr_oauth_cache option to FALSE.
    
    0 讨论(0)
  • 2020-12-03 18:17

    I don't know much about it.

    But if it's in batch, maybe try this:

    doit <- function(<snip>) {
        <snip>
        # connect to Twitter
        setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
        < echo 1
        <snip>
    }
    

    Also have you tried posting the 1 outside the function to see if it does the same?

    And maybe it will work if you put the 1 under the snip

    These are just suggestions, cause i don't know very much about the topic, but it might help though.

    0 讨论(0)
提交回复
热议问题