【问题标题】:Rstudio to Twitter handshake giving Error: Unauthorized via twitterRRstudio 与 Twitter 握手给出错误:未经授权通过 twitterR
【发布时间】:2014-03-16 03:27:16
【问题描述】:

我正在尝试使用 twitteR 向 Twittter 验证 Rstudio。

我只在握手后出错。

我的代码是:

#install(rcurl, ROAuth, twitteR)
#load(rcurl, ROAuth, 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 <- "key"
consumerSecret <- "secret key"
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()

我得到了握手,它把我送到了 Twitter 的身份验证页面。我授权并得到一个密码。

我在这段代码中输入了密码:

registerTwitterOAuth(pin number here)

然后我返回“错误:未经授权”。

我添加了一个新的 Twitter 应用程序帐户并获得了一组不同的客户密钥,但仍然是相同的。

【问题讨论】:

  • 请注意,您没有向 Twitter 验证“RStudio”,只是 R。RStudio 只是您用来编辑和运行代码的 IDE。 (同样,你不会说“将 iPython 连接到...”)
  • 我提到了 RStudio,因为我看到有时它在使用 twitter 时会出现自己的问题。不知道为什么。
  • 更新:我在常规的 R 领事馆中这样做了,并且更进一步。现在我在搜索twitter时出错: searchTwitter('patriots', geocode='42.375,-71.1061111,10mi') Error in twInterfaceObj$doAPICall(cmd, params, "GET", ...) : OAuth authentication is required with Twitter 的 API v1.1

标签: r twitter twitter-oauth rstudio


【解决方案1】:

您可以按照以下步骤操作:

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 的最新版本是 1.1.7 => http://cran.r-project.org/web/packages/twitteR/index.html

【讨论】:

    【解决方案2】:

    在尝试了网络上所有可能资源的建议后,按照以下步骤使我的代码正常工作!!!!下面的代码我使用的是 Windows 7、64 位和 RStudio

    确保您已安装所有适当的软件包 - ROAuth、twitteR、RCurl;通常,CRAN 页面上指示的依赖项会将您指向丢失的包(如果有)

    如代码所示全局设置 SSL 证书;对于为什么需要这样做,我不是专家,但它>帮助我通过使用 download.file 命令下载 cacert.pem 文件获取我的代码 确保您的 twitter 应用程序具有完全读写访问权限,而不仅仅是只读访问权限是默认的

    与 Twitter 链接的最终代码

    获取合适的库

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

    全局设置 SSL 证书

       options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
    
       download.file(url="http://curl.haxx.se/ca/cacert.pem", destfile="cacert.pem")
    

    确保网址是 https 而不是 http,以防您从某个地方复制了代码

        reqURL <- "https://api.twitter.com/oauth/request_token"
        accessURL <- "https://api.twitter.com/oauth/access_token"
        authURL <- "https://api.twitter.com/oauth/authorize"
    
    
        consumerKey <- "====="
    
        consumerSecret <- "====="
    
    
        twitCred <- OAuthFactory$new(consumerKey=consumerKey,
                                     consumerSecret=consumerSecret,
                                     requestURL=reqURL,
                                     accessURL=accessURL,
                                     authURL=authURL)
    
        #twitCred <- getTwitterOAuth(consumer_key= consumerKey, consumer_secret=consumerSecret)
    
    
        twitCred$handshake()
    

    此时,您需要将 URL 粘贴到浏览器中,这将使用您的 > 应用程序打开一个 twitter 页面 - 您需要从那里获取 PIN 并将其粘贴到 R 控制台中

     registerTwitterOAuth(twitCred)
    

    测试代码:现在你可以测试代码了

    testSearch = searchTwitter("#food", n = 10, cainfo="cacert.pem")
    Moditweets <- searchTwitter("#Narendra Modi", n = 100)
    

    【讨论】:

    • 把 cmets 转成普通文本,这样可读性会好很多。
    【解决方案3】:

    以下对我有用,Windows 8,请注意必须在 cmd 中进行身份验证,不能通过 Rstudio IDE 进行,您希望将 R 作为语言进行身份验证

    #Open admin shell: In the Start Search box, type cmd, and then press CTRL+SHIFT+ENTER.
    #Goto C:/...R../bin/x64, run ./r.exe
    
    install.packages("twitteR") #Takes some time for mirrors to pop-up
    library(twitteR)
    library(ROAuth)
    library(RCurl)
    download.file(url="http://curl.haxx.se/ca/cacert.pem",destfile="cacert.pem")
    requestURL <- ...
    accessURL <- ...
    authURL <- ...
    consumerKey <- ...
    consumerSecret <- ...
    twitCred <- OAuthFactory$new(consumerKey=consumerKey, consumerSecret=consumerSecret,
                                 requestURL=requestURL, accessURL=accessURL, authURL=authURL)
    twitCred$handshake(cainfo="cacert.pem")
    registerTwitterOAuth(twitCred)
    save(list="twitCred", file="twitteR_credentials")
    ## Now to get some tweets, place the files cacert.perm and 
    
    twitteR_credentials from bin folder to the the current working directory of Rstudio
    library (twitteR)
    load("twitteR_credentials")
    registerTwitterOAuth(twitCred)
    searchTwitter('#Obama', cainfo="cacert.pem", n=5, locale = 'en')
    

    【讨论】:

      猜你喜欢
      • 2014-03-08
      • 2013-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 2018-05-16
      • 2019-07-07
      • 1970-01-01
      相关资源
      最近更新 更多