Twitter,错误:401 访问“/1/statuses/sample.json".原因:未经授权 [英] Twitter, Error: 401 accessing '/1/statuses/sample.json'. Reason: Unauthorized

查看:42
本文介绍了Twitter,错误:401 访问“/1/statuses/sample.json".原因:未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想下载推文(不搜索特定问题).我尝试了你的建议:

curlPerform(url = https://stream.twitter.com/1/statuses/sample.json -u USER:PASSWORD -o "somefile.txt"#设置目录setwd("C:\\")#### 将输出重定向到文件WRITE_TO_FILE <- 函数(x){如果 (nchar(x) > 0 ) {write.table(x, file="Twitter Stream Capture.txt", append=T, row.names=F, col.names=F)}}### windows 用户需要获取此证书才能进行身份验证下载.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")### 将来自 Twitter Firehouse 的原始 JSON 数据写入文本文件getURL("https://stream.twitter.com/1/statuses/sample.json",cainfo = "cacert.pem",写=WRITE_TO_FILE)

仅当我取消 'userpwd="Username:Password' 我得到一个结果,它是一个包含以下信息的文本文件:

<头><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>错误 401 未经授权</title><身体><h2>HTTP 错误:401</h2><p>访问/1/statuses/sample.json"时出现问题.原因:<预>未经授权的

我希望完全留在 R 中并且需要使用 Windows.关于如何解决这个问题有什么建议吗?

提前致谢

解决方案

尝试使用 userpwd 参数指定用户名和密码:

库(RCurl)WRITE_TO_FILE <- 函数(x){如果(nchar(x)> 0){write.table(x, file='twitter_stream_capture.txt', append=TRUE,row.names=FALSE,col.names=FALSE)}}下载文件(url='http://curl.haxx.se/ca/cacert.pem', destfile='cacert.pem')getURL('https://stream.twitter.com/1/statuses/sample.json',userpwd='用户名:密码', cainfo='cacert.pem',写=WRITE_TO_FILE)

用有效的 Twitter 用户名和密码替换 getURL 中的 usernamepassword.

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

解决方案

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.

这篇关于Twitter,错误:401 访问“/1/statuses/sample.json".原因:未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆