在输入Twitter API密码后,twitteR会抛出禁止错误。

6
我正在使用R和twitteR包分析一些推文。握手和注册代码似乎正常工作,并且我从R获得了授权链接。然而,当我输入从https://api.twitter.com/oauth/authorize获取的PIN时,出现“Forbidden Error”。希望能得到帮助。
代码:
TwitterOAuth<-function(){
  reqURL <- "https://api.twitter.com/oauth/request_token"
  accessURL <- "http://api.twitter.com/oauth/access_token"
  authURL <- "http://api.twitter.com/oauth/authorize"
  consumerKey <- "xxxxxxxxxxxxxxxx"
  consumerSecret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                             consumerSecret=consumerSecret,
                             requestURL=reqURL,
                             accessURL=accessURL,
                             authURL=authURL)
  options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package =  "RCurl")))
  twitCred$handshake()
  registerTwitterOAuth(twitCred)
}

响应:

TwitterOAuth() 要建立连接,请将您的网络浏览器指向: http://api.twitter.com/oauth/authorize?oauth_token=X0AwET4FXBC7YRIWWN3iF61WFNE1DjxbfibqtfFjgcc
完成后,请记录给您的PIN并在此处提供:1998913
错误:禁止访问

我的sessionInfo()

R版本3.0.2(2013-09-25) 平台:x86_64-w64-mingw32 / x64(64位)

语言环境: [1] LC_COLLATE = Turkish_Turkey.1254 LC_CTYPE = Turkish_Turkey.1254 LC_MONETARY = Turkish_Turkey.1254 [4] LC_NUMERIC = C LC_TIME = Turkish_Turkey.1254

附加的基本包: [1] stats graphics grDevices utils datasets methods base

其他附加的包: [1] twitteR_1.1.7 rjson_0.2.13 ROAuth_0.9.3 digest_0.6.4 RCurl_1.95-4.1 bitops_1.0-6

通过名称空间加载(未附加): [1] tools_3.0.2

5个回答

24

将您的访问网址从http更改为https。


这对我解决了问题。@user3265306 如果你还在,并且这个方法对你有效,请将其标记为答案。 - Ari B. Friedman
我也试过了,谢谢@Martin!请@user3265306将其标记为答案! - QuantumGorilla

7
您可以按照以下步骤操作(别忘了,有效的 URL 使用 https):
reqURL <- "https://api.twitter.com/oauth/request_token"
accessURL <- "https://api.twitter.com/oauth/access_token"
authURL <- "https://api.twitter.com/oauth/authorize"
consumerKey <- "Mjn6tdsadsadkasdklad2SV1l"
consumerSecret <- "58Z7Eldsdfaslkf;asldsaoeorjkfksaVCQtvri"
twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                             consumerSecret=consumerSecret,
                             requestURL=reqURL,
                             accessURL=accessURL,
                             authURL=authURL)
twitCred$handshake()

运行这段代码后,你会在 R 控制台上看到如下信息:
To enable the connection, please direct your web browser to: 
https://api.twitter.com/oauth/authorize?oauth_token=scmVODruosvz6Tdsdadadasdsa
When complete, record the PIN given to you and provide it here:

只需将链接粘贴到您的浏览器中,然后授权该应用程序,最后您将获得PIN码,只需将PIN码复制并粘贴到您的R控制台中即可。

registerTwitterOAuth(twitCred)

R控制台将显示TRUE,如果您成功了。

user <- getUser("xxx")
userTimeline(user, n=20, maxID=NULL, sinceID=NULL, includeRts=FALSE)

当我在URL中使用https时,我会收到一个“Error in strsplit(response, "&") : non-character argument”错误。 - StrawhatLuffy

3
根据这篇博客:http://thinktostart.wordpress.com/2013/05/22/twitter-authentification-with-r/,介绍了与R语言结合使用的Twitter认证相关技术。
library(RCurl)
# Set SSL certs globally
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))

require(twitteR)
reqURL <- "https://api.twitter.com/oauth/request_token"

accessURL <- "https://api.twitter.com/oauth/access_token"

authURL <- "https://api.twitter.com/oauth/authorize"

consumerKey <- "yourconsumerkey"

consumerSecret <- "yourconsumersecret"

twitCred <- OAuthFactory$new(consumerKey=consumerKey,consumerSecret=consumerSecret,requestURL=reqURL,accessURL=accessURL,authURL=authURL)

twitCred$handshake(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))

registerTwitterOAuth(twitCred)

0
Cookies可能是问题所在:我遇到了与我相同的情况,当我在不同的浏览器中按照twitcred$handshake()给出的链接操作后,问题得以解决;也就是说,我一开始是在Safari中尝试,但之后在Chrome中尝试,然后在R中接收到的PIN码就能够被成功接受。

0

我也遇到了同样的“错误:禁止”响应,今天还没有解决(尽管我之前已经解决了)。

我的Windows 8.1系统的R脚本还包括以下行:

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

我在网上找到了这行代码,并附有注释:“这是Windows必要的步骤”

大约三天前,我成功地输入了在线PIN码后进入Twitter,但今天我无法进入。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接