2014-02-10 181 views
0

我注意到许多关于Twitter API身份验证问题的主题,但显然没有人与我的问题相关。只要我尝试进行身份验证,在我被要求输入PIN码后,系统就会抛出一个错误(“未经授权”)。发生这种情况之前我可以输入PIN码。Twitter OAuth未授权错误

的代码是:

library("twitteR") 
library("RCurl") 
library("ROAuth") 

# Set SSL certs globally 
options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))) 

Credentials <- OAuthFactory$new(
    consumerKey = "XX", 
    consumerSecret = "XX", 
    oauthKey = "XX", 
    oauthSecret = "XX", 
    requestURL = "https://api.twitter.com/oauth/request_token", 
    authURL = "https://api.twitter.com/oauth/authorize", 
    accessURL = "https://api.twitter.com/oauth/access_token") 

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

而且结果是:

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

正如上面提到的,这种情况发生之前,其实我可以输入个人识别码。我在RStudio中运行脚本,但在传统的R GUI上运行并没有任何改变。我正在运行R版本3.0.1。

Post Scriptum 我尝试了不同版本的代码,例如this one,但我得到了完全相同的错误。

回答

2

您可以按照此步骤:

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) 

如果仍然有任何问题只是尽量展示你的包版本并更新到最新版本

sessionInfo() 
update.packages("twitteR") 

Twitter的最后一个版本是1.1.7 =>http://cran.r-project.org/web/packages/twitteR/index.html

您可以下载twitteR手册=>见第12页http://cran.r-project.org/web/packages/twitteR/twitteR.pdf

相关问题