使用ROAuth进行twitteR代理身份验证

时间:2013-11-26 05:08:15

标签: r twitter proxy twitter-oauth roauth

我正在尝试使用来自RStudio的twitteR软件包 但是我收到了一个错误:

  

'需要代理身份验证',有时 - “无法找到主机”。

有许多线程存在同样的问题 我尝试过所有事情(--internet2,R by setting "~/Rgui.exe" http_proxy=http:/999.99.99.99:8080/ http_proxy_user=ask)。但它没有用。

在我的下面找到我的代码(我在办公室桌面上运行,RStudio),这需要身份验证:proxyuserpwd="**domain//username:pwd**"

我不确定这条线。我尝试了各种组合但没有效果。

rm(list=ls())
library(twitteR)
library(ROAuth)
library(RCurl)

options(RCurlOptions = list(
  verbose = TRUE,
  proxy ="http://proxy1.domain.com:8080",
  proxyuserpwd="domain//username:pwd",
  proxyauth="ntlm"))

reqURL <- "http://api.twitter.com/oauth/request_token"
accessURL <- "http://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
consumerKey <- '.............'
consumerSecret <- '..........'
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                          consumerSecret=consumerSecret,
                         requestURL=reqURL,
                         accessURL=accessURL,
                         authURL=authURL)

download.file(url="http://curl.haxx.se/ca/cacert.pem",destfile="cacert.pem")
twitCred$handshake(cainfo="cacert.pem")



< Proxy-Authenticate: BASIC realm="Access to this server requires AD Authentication.    Prefix your user ID with domain."
Host: api.twitter.com
Accept: */*
Proxy-Connection: Keep-Alive
Content-Length: 221
Content-Type: application/x-www-form-urlencoded

Error: Proxy Authentication Required
> registerTwitterOAuth(twitCred)
< HTTP/1.1 407 Proxy Authentication Required
* Authentication problem. Ignoring this.
< Proxy-Authenticate: NTLM
< Cache-Control: no-cache
< Pragma: no-cache
< Content-Type: text/html; charset=utf-8
* HTTP/1.1 proxy connection set close!
< Proxy-Connection: close
< Set-Cookie: BCSI-CS-3bf0ea03406bba28=2; Path=/
< Connection: close
< Content-Length: 899
< 
* Closing connection #0
Error in registerTwitterOAuth(twitCred) : 
oauth has not completed its handshake

然而,当我尝试使用以下代码时。

library("RCurl")
opts <- list(
proxy         = "proxy1.domain.com", 
proxyusername = "domain", 
proxypassword = "pwd",
proxyport     = 8080
)
getURL("http://stackoverflow.com", .opts = opts)

......我很成功。

>sessionInfo()  
R version 2.15.1 (2012-06-22)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] twitteR_1.1.7  rjson_0.2.13   ROAuth_0.9.3   digest_0.6.3   RCurl_1.95-4.1    bitops_1.0-5  

loaded via a namespace (and not attached):
[1] tools_2.15.1

我从这里尝试了多线程解决方案,他们的解决方案都没有用。我不知道我在哪里犯了错误。(这是我需要用域名困扰我的域名吗?)有人可以解决这个问题。

更新

当我在RGui中运行相同的代码(设置“〜/ Rgui.exe”http_proxy = http:/999.99.99.99:8080 / http_proxy_user = ask)时,它提示输入用户名和密码来下载cacert.pem。<登记/> 但是当我尝试握手时,它会抛出同样的错误:

  

错误:需要代理身份验证。

更新

我收到以下错误:
Error in function (type, msg, asError = TRUE) : Received HTTP code 407 from proxy after CONNECT

更新 正如托马斯建议的那样 - 试过

reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
consumerKey <- '-----'
consumerSecret <- '-------'
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                         consumerSecret=consumerSecret,
                         requestURL=reqURL,
                         accessURL=accessURL,
                         authURL=authURL)

h <- getCurlHandle(
proxy         = "proxy1.domain.com", 
proxyusername = "username",  #but i have to always prefix my domain with username(Domain\username or domain\\username or domain//username)
proxypassword = "password",
proxyport     = 8080,
cainfo = "cacert.pem")
twitCred$handshake(curl=h)

我收到以下错误
Error in strsplit(response, "&") : non-character argument
我正在用追溯输出更新链接
5: strsplit(response, "&")
4: lapply(X = X, FUN = FUN, ...)
3: sapply(strsplit(response, "&")[[1]], strsplit, "=")
2: parseResponse(resp)
1: twitCred$handshake(curl = h)

3 个答案:

答案 0 :(得分:2)

感谢托马斯,为了你的帮助 - 我解决了问题。

rm(list=ls())
library(twitteR)
library(ROAuth)
library(RCurl)

reqURL <- "http://api.twitter.com/oauth/request_token"
accessURL <- "http://api.twitter.com/oauth/access_token"
authURL <- "http://api.twitter.com/oauth/authorize"
consumerKey <- '-----'
consumerSecret <- '------'
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                          consumerSecret=consumerSecret,
                         requestURL=reqURL,
                         accessURL=accessURL,
                         authURL=authURL)

h <- getCurlHandle(
proxy         = "proxy1.domain.com", 
 proxyusername = "username", 
proxypassword = "pwd",
proxyport     = 8080,     cainfo = "cacert.pem")

twitCred$handshake(curl=h)

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

我已用http

替换了https

答案 1 :(得分:1)

我真的对代理服务器一无所知,但为什么不为握手线尝试这个(明确指定curl句柄):

h <- getCurlHandle(
      proxy         = "proxy1.domain.com", 
      proxyusername = "domain", 
      proxypassword = "pwd",
      proxyport     = 8080,
      cainfo = "cacert.pem")
twitCred$handshake(curl=h)

答案 2 :(得分:0)

我也在代理服务器后面工作,并且能够使用以下代码访问twitter。

consumer_key <- '<put your consumer key here>'
consumer_secret <- '<put your consumer secret here>'
access_token <- "<put your access token here>"
access_secret <- "<put your access token secret here>"
set_config(use_proxy(url='proxy- address',port-number, username, password))
setup_twitter_oauth(consumer_key,consumer_secret, access_token , access_secret)

能够获取推文。我还写了一个博客,一步一步的程序来解决这个问题。您可以从http://analyticsinall.blogspot.in/search/label/Analytics%20for%20Business

访问它