Maria Molina
Maria Molina

Reputation: 33

Twitter, Error: 401 accessing '/1/statuses/sample.json'. Reason: Unauthorized

I want to download tweets (without searching an specific question). I tried your advice:

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)

Only if I suppress 'userpwd="Username:Password' I get a result, which is a text file containing the following information:

<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</pre>

I am looking to stay completely within R and need to use Windows. Any advice on how to solve this problem?

Thanks in advance

Upvotes: 2

Views: 1923

Answers (1)

jbaums
jbaums

Reputation: 27388

Try specifying the username and password with the userpwd argument:

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)

Replace username and password in getURL with a valid Twitter username and password.

Upvotes: 2

Related Questions