1

Have successfully connected to Twitter using R and trying to leverage that code to connect to LinkedIn, but not quite able to make it work.

I think the code is close, but somehow the last step is returning an error.

Currently, I am able to get LinkedIn to return a token right after the handshake, but when I query it in the browser, I get an error from linkedIn saying that a client_id is required.

https://www.linkedin.com/uas/oauth2/authorization?oauth_token=<...>

The R code to create the OAuth file is below with the keys provided by LinkedIn redacted out.

rm(list=ls())

library(ROAuth)

reqURL <- "https://api.linkedin.com/uas/oauth/requestToken"
accessURL <- "https://api.linkedin.com/uas/oauth2/accessToken"
authURL <- "https://www.linkedin.com/uas/oauth2/authorization"
consumerKey <- "<...>"
consumerSecret <- "<...>"
#oAuthKey <- "<...>"
#oAuthSecret <- "<...>"

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

linkedInCred$handshake()

credentials$OAuthRequest(testURL, "GET")


save(linkedInCred, file="credentials.RData")
hnreddy
  • 63
  • 1
  • 6

1 Answers1

4

Use httr instead. Hadley has an example in his package: https://github.com/hadley/httr/blob/master/demo/oauth2-linkedin.r

Here's example

library("httr")
myapp <- oauth_app(appname = "scottsapp", key = "<key>", secret = "<secret>")
TokenLinkedIn <- R6::R6Class("TokenLinkedIn", inherit = Token2.0, list(
  sign = function(method, url) {
    url <- parse_url(url)
    url$query$oauth2_access_token <- self$credentials$access_token
    list(url = build_url(url), config = config())
  }
))
token <- TokenLinkedIn$new(endpoint = oauth_endpoints("linkedin"), app = myapp)
sckott
  • 5,755
  • 2
  • 26
  • 42
  • Thanks! Seems to be leading me in the right direction, though, the Token2.0 is giving me a problem. I have installed the R6 package and library; but getting an error message with Token2.0 - "Token2.0" not found. Looking through Hadley W.'s github and googling around, but have not found the solution to this issue. – hnreddy Nov 25 '14 at 21:38
  • @hnreddy `Token2.0` should be in `httr`, see `httr::Token2.0`. Maybe you need to upgrade `httr`? I have version `0.5.0.900` – sckott Nov 25 '14 at 22:06
  • Thanks - This may be the issue. Using packageVersion("httr"), I am returning '0.5'. So this is definitely different than your 0.5.0.900, however, when I went to CRAN (http://cran.r-project.org/), the latest version is also '0.5'. I guess I need to figure out how to pull httr from Hadley's github? (https://github.com/hadley/httr)? – hnreddy Nov 25 '14 at 22:32
  • Yeah, just checked and it looks like `Token2.0` has changed slightly since `0.5.0`. Install from Github like `devtools::install_github("hadley/httr")` – sckott Nov 25 '14 at 22:38
  • ok - thanks. Started down the path and installed the devtools package and library, but using a Mac, so need to figure out how to get make working ... let me figure that out and cross my fingers that this works. – hnreddy Nov 25 '14 at 23:01
  • Thanks - installed the commandline tools in XCode (http://stackoverflow.com/questions/6767481/where-can-i-find-make-program-for-mac-os-x-lion) and then modified my Makeconf (http://stackoverflow.com/questions/19533220/cannot-install-r-package-from-source-in-mac-osx-maverick) – hnreddy Nov 26 '14 at 00:36
  • But, ran into another issue when running Hadley's script on the "new" method. Specifically - Error in stopifnot(is.oauth_endpoint(endpoint), is.oauth_app(app), is.list(params)) : could not find function "is.oauth_endpoint" – hnreddy Nov 26 '14 at 00:38
  • Unfortunately - was not able to get this to work. At the moment not sure if the issue is with the library, my code / setup, or linkedIn. The code is stuck and fails on this line - token <- TokenLinkedIn$new(endpoint = oauth_endpoints("linkedin"), app = myapp) and returns this error - Error in stopifnot(is.oauth_endpoint(endpoint), is.oauth_app(app), is.list(params)) : could not find function "is.oauth_endpoint" – hnreddy Nov 26 '14 at 13:44
  • weird, `is.oauth_endpoint` is a function inside `httr`, it's not exported, but it should be there is you are using `v0.5.0.9000` – sckott Nov 26 '14 at 15:20
  • yeah - I posted an issue on github - https://github.com/hadley/httr/issues/171 - For the moment, I'll assume that the httr code works and somehow I have some sort of contention between 0.5 and 0.5.0.9000 - am using RStudio and not quite sure how to resolve the contention issue since I don't tend to go this deep in the code stack. – hnreddy Nov 26 '14 at 15:54
  • cleaned up the code - to make doubly sure that I have 0.5.0.9000 installed. ran the function ls("package:httr") and don't see the function 'is.oauth_endpoint' within it. Am now thinking that the function is simply missing?? – hnreddy Nov 26 '14 at 16:19