簡體   English   中英

使用R通過Twitter的API進行OAuth身份驗證

[英]OAuth authentication with Twitter's API using R

我正在探索Twitter API。

我在R中使用ROAuthtwitteR軟件包。

我認為事情進展順利:

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

到現在為止還挺好。 現在,我准備看到一些Twitter時間表:

> my_tweets <- userTimeline('someTimeline')

不幸的是,我得到:

Error in InterfaceObj$doAPICall(cmd, params, method, ...) : 
  OAuth authentication is required with Twitter's API v1.1

我一直在研究這意味着什么。 我覺得我的OAuth身份驗證是適當的。 為什么會出現此錯誤?

我使用的API版本是否存在問題?

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

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

我發現您已成功訪問(又稱握手)。 我假設您的握手代碼將類似於以下行。

Cred$handshake(cainfo = system.file("CurlSSL", "your_certificate.pem", package = "RCurl") )

然后,我假定您使用以下行注冊了Twitter OAuth,您說這是成功的。

registerTwitterOAuth(Cred)

然后,您的userTimeline應該包括您之前創建的pem文件。

my_tweets <- userTimeline('someTimeline', cainfo="your_certificate.pem", n=200)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM