Твиттер, ошибка: 401 имеет доступ к /1/statuses/sample.json. Причина: несанкционированный - PullRequest
2 голосов
/ 13 февраля 2012

Я хочу скачать твиты (без поиска конкретного вопроса). Я попробовал ваш совет:

curlPerform(url = https://stream.twitter.com/1/statuses/sample.json -u USER:PASSWORD -o "somefile.txt"

# set the directory
setwd("C:\\")

#### redirects output to a file
WRITE_TO_FILE <- function(x) {
  if (nchar(x) >0 ) {
    write.table(x, file="Twitter Stream Capture.txt", append=T, row.names=F, col.names=F)
  }
}

### windows users will need to get this certificate to authenticate
download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")

### write the raw JSON data from the Twitter Firehouse to a text file
getURL("https://stream.twitter.com/1/statuses/sample.json", 
       cainfo = "cacert.pem", 
       write=WRITE_TO_FILE)

Только если я подавляю 'userpwd = "Имя пользователя: Пароль', я получаю результат, который представляет собой текстовый файл, содержащий следующую информацию:

<code><html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing '/1/statuses/sample.json'. Reason:
<pre>    Unauthorized

Я хочу остаться полностью внутри R и должен использовать Windows. Любой совет, как решить эту проблему?

Заранее спасибо

1 Ответ

2 голосов
/ 14 февраля 2012

Попробуйте указать имя пользователя и пароль с аргументом userpwd:

library(RCurl)

WRITE_TO_FILE <- function(x) {
  if (nchar(x) > 0) {
    write.table(x, file='twitter_stream_capture.txt', append=TRUE, 
                row.names=FALSE, col.names=FALSE)
  }
}

download.file(url='http://curl.haxx.se/ca/cacert.pem', destfile='cacert.pem')

getURL('https://stream.twitter.com/1/statuses/sample.json', 
       userpwd='username:password', cainfo='cacert.pem',
       write=WRITE_TO_FILE)

Замените username и password в getURL действительными именем пользователя и паролем Twitter.

...