使用twitteR的自动httr认证,提供对“批处理"中的交互式提示的响应.模式 [英] Automated httr authentication with twitteR , provide response to interactive prompt in "batch" mode
问题描述
我正在使用R包twitteR将项目发布到Twitter.我把所有东西都放进了一个函数中,并且一切正常.但是,我希望在不提示您响应的情况下运行该函数,而且我还没有弄清楚该怎么做.有什么建议吗?
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 prompted for a response, and I haven't figured out how to do that. Any suggestions?
这是我功能的基本内容:
Here are the bare bones of my function:
doit <- function(<snip>) {
<snip>
# connect to Twitter
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
<snip>
}
从命令行运行该功能时,系统会提示我进行交互式响应.
When I run the function from the command line, I am prompted for an interactive response.
[1] "Using direct authentication"
Use a local file to cache OAuth access credentials between R sessions?
1: Yes
2: No
Selection:
当setup_twitter_oauth()函数不在函数外部时,我可以在脚本中直接提供此信息,方法是在下一行中输入我的响应,这与对其他用户输入函数(如readline()或scan( ).
I can provide this information directly in a script when the setup_twitter_oauth() function is outside of a function, by entering my response in the following line, much like can be done for other user input functions like readline() or scan().
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
1
但是,当setup_twitter_oauth()位于函数的内部时,我无法使用这种方法.
However, I haven't been able to get this approach to work when setup_twitter_oauth() is INSIDE of a function.
对于在不要求用户输入的情况下如何运行它的任何建议,我们将不胜感激.
I would appreciate any suggestions on how to get this to run without requiring user input.
=====
下面来自@NicE的答案可以解决问题.我将选项设置合并到我的函数中,如下所示:
The answer from @NicE below did the trick. I incorporated the options setting in my function as:
doit <- function(<snip>) {
<snip>
# connect to Twitter
origop <- options("httr_oauth_cache")
options(httr_oauth_cache=TRUE)
setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)
options(httr_oauth_cache=origop)
<snip>
}
推荐答案
您可以尝试将httr_oauth_cache
选项设置为TRUE:
You can try setting the httr_oauth_cache
option to TRUE:
options(httr_oauth_cache=T)
twitteR软件包使用httr
软件包,在该软件包的Token
手册页上,它们提供了有关缓存的提示:
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.
这篇关于使用twitteR的自动httr认证,提供对“批处理"中的交互式提示的响应.模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!