r-lib / httr

httr: a friendly http package for R
https://httr.r-lib.org
Other
986 stars 1.99k forks source link

problem with LinkedIn integration #171

Closed harishnreddy closed 9 years ago

harishnreddy commented 10 years ago

Trying to use httr to integrate with LinkedIn and failing when trying to generate a token. Am still a beginner in terms of the innards of R - so not quite sure where to turn.

Am using the demo example as my reference and this is the error that I am getting ->

Error in stopifnot(is.oauth_endpoint(endpoint), is.oauth_app(app), is.list(params)) : could not find function "is.oauth_endpoint"

The following is my code:

#install.packages("R6")
#install.packages("ROAuth")
#install.packages("httr")
#install.packages("devtools")

#must be 0.5.0900
library(devtools)
library(httr)
library(R6)
#library(oauth-token)
library(RCurl)
library(methods)

devtools::install_github("hadley/httr")

#1. Find OAuth settings for linkedin:
#    https://developer.linkedin.com/documents/linkedins-oauth-details
oauth_endpoints("linkedin")

#2. Register an application at https://www.linkedin.com/secure/developer
#    Insert your values below - if secret is omitted, it will look it up in
#    the LINKEDIN_CONSUMER_SECRET environmental variable. Make sure to
#    register http://localhost:1410/ as an "OAuth 2.0 Redirect URL".
#    (the trailing slash is important!)
myapp <- oauth_app("linkedin", key = "XXX", secret="XXX")

#3. Get OAuth credentials
# LinkedIn doesn't implement OAuth 2.0 standard
# (http://tools.ietf.org/html/rfc6750#section-2) so we extend the Token2.0
# ref class to implement a custom sign method.
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)

req <- GET("https://api.linkedin.com/v1/people/~", config(token = token))
stop_for_status(req)
content(req)
hadley commented 10 years ago

Is that literally the sequence of commands that ran? Because once you've loaded httr, installing it is likely to cause problems. I'd recommend starting from a clean R session, installing the packages, and then restarting again. Does the problem still occur?

harishnreddy commented 10 years ago

Thanks - I am using RStudio (0.98.490) on a Mac. I reworked the code and still have the issue.

1) I closed RStudio and did not save the workspace and then restarted RStudio. 2) After re-starting RStudio, I cleared all variables. 3) I ran sessionInfo() and can see that httr is not installed 4) I then download httr from github 5) and install the httr library 6) sessionInfo confirms that I have 0.5.0.9000 and looking at the package list confirms this. 7) the code runs until this command

token <- TokenLinkedIn$new(endpoint = oauth_endpoints("linkedin"), app = myapp) 

The error message is 

Error in stopifnot(is.oauth_endpoint(endpoint), is.oauth_app(app), is.list(params)) : 
  could not find function "is.oauth_endpoint"

8) In R Studio, I then looked in the list of packages and functions associated with it and do not see is.oauth_endpoint as an available function.

The code I used is just below

rm(list=ls())

sessionInfo()

#install.packages("R6")
#install.packages("ROAuth")
#install.packages("httr")
#install.packages("devtools")

#must be 0.5.0.900
library(devtools)
library(R6)
library(RCurl)

devtools::install_github("hadley/httr")

library("httr", lib.loc="/Library/Frameworks/R.framework/Versions/3.0/Resources/library")

sessionInfo()

#1. Find OAuth settings for linkedin:
#    https://developer.linkedin.com/documents/linkedins-oauth-details
oauth_endpoints("linkedin")

# 2. Register an application at https://www.linkedin.com/secure/developer
#    Insert your values below - if secret is omitted, it will look it up in
#    the LINKEDIN_CONSUMER_SECRET environmental variable. Make sure to
#    register http://localhost:1410/ as an "OAuth 2.0 Redirect URL".
#    (the trailing slash is important!)
myapp <- oauth_app("linkedin", key = "xxx", secret="xxx")

# 3. Get OAuth credentials
# LinkedIn doesn't implement OAuth 2.0 standard
# (http://tools.ietf.org/html/rfc6750#section-2) so we extend the Token2.0
# ref class to implement a custom sign method.
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)

req <- GET("https://api.linkedin.com/v1/people/~", config(token = token))
stop_for_status(req)
content(req)
hadley commented 10 years ago
harishnreddy commented 10 years ago

Reopened the issue and hopefully fixed the output so the syntax is better.

In terms of rm(list=ls(), I do this because I have a lot of different scripts and want to make sure that my variables and environment are clean for each script - especially if I switch from one script to another in RStudio. Haven't found a better way to manage this.

Thanks - I am using RStudio (0.98.490) on a Mac. I reworked the code and still have the issue.

1) I closed RStudio and did not save the workspace and then restarted RStudio. 2) After re-starting RStudio, I cleared all variables. 3) I ran sessionInfo() and can see that httr is not installed 4) I then download httr from github 5) and install the httr library 6) sessionInfo confirms that I have 0.5.0.9000 and looking at the package list confirms this. 7) the code runs until this command

token <- TokenLinkedIn$new(endpoint = oauth_endpoints("linkedin"), app = myapp) 

The error message is

Error in stopifnot(is.oauth_endpoint(endpoint), is.oauth_app(app), is.list(params)) : 
  could not find function "is.oauth_endpoint"

8) In R Studio, I then looked in the list of packages and functions associated with it and do not see is.oauth_endpoint as an available function.

The code I used is just below

rm(list=ls())

sessionInfo()

#install.packages("R6")
#install.packages("ROAuth")
#install.packages("httr")
#install.packages("devtools")

#must be 0.5.0.900
library(devtools)
library(R6)
library(RCurl)

devtools::install_github("hadley/httr")

library("httr", lib.loc="/Library/Frameworks/R.framework/Versions/3.0/Resources/library")

sessionInfo()

#1. Find OAuth settings for linkedin:
#    https://developer.linkedin.com/documents/linkedins-oauth-details
oauth_endpoints("linkedin")

# 2. Register an application at https://www.linkedin.com/secure/developer
#    Insert your values below - if secret is omitted, it will look it up in
#    the LINKEDIN_CONSUMER_SECRET environmental variable. Make sure to
#    register http://localhost:1410/ as an "OAuth 2.0 Redirect URL".
#    (the trailing slash is important!)
myapp <- oauth_app("linkedin", key = "xxx", secret="xxx")

# 3. Get OAuth credentials
# LinkedIn doesn't implement OAuth 2.0 standard
# (http://tools.ietf.org/html/rfc6750#section-2) so we extend the Token2.0
# ref class to implement a custom sign method.
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)

req <- GET("https://api.linkedin.com/v1/people/~", config(token = token))
stop_for_status(req)
content(req)
hadley commented 10 years ago

Variables are only one small way that state is transfer across sessions. You are much better off never saving or loading your workspace, and regularly using Session | Restart R.

harishnreddy commented 10 years ago

Thanks.

Will make the switch - would be nice to have something similar at the command line - so it could be a part of the script - though there are probably other issues with that.

That said, still have the same issue in the code.

hadley commented 9 years ago

It works for me. Could you please send the output of traceback() after the error, and devtools::session_info()?

harishnreddy commented 9 years ago

I tried it again and still getting the same issue.

> traceback() 3: stopifnot(is.oauth_endpoint(endpoint), is.oauth_app(app), is.list(params)) 2: public_env$initialize(...) 1: TokenLinkedIn$new(endpoint = oauth_endpoints("linkedin"), app = myapp)

> devtools::session_info()
Session info-------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.0.2 (2013-09-25)
 system   x86_64, darwin10.8.0        
 ui       RStudio (0.98.490)          
 language (EN)                        
 collate  en_US.UTF-8                 
 tz                                   

Packages-----------------------------------------------------------------------------------
 package    * version    date       source                      
 bitops     * 1.0.6      2013-08-17 CRAN (R 3.0.1)              
 devtools   * 1.6.1      2014-10-07 CRAN (R 3.0.2)              
 evaluate     0.5.3      2014-03-29 CRAN (R 3.0.2)              
 formatR      0.10       2013-10-26 CRAN (R 3.0.2)              
 httr       * 0.5.0.9000 2014-12-05 Github (hadley/httr@d9a63e0)
 knitr        1.5        2013-09-28 CRAN (R 3.0.2)              
 R6         * 1.0.1      2014-07-17 CRAN (R 3.0.2)              
 RCurl      * 1.95.4.1   2013-03-06 CRAN (R 3.0.2)              
 rstudioapi   0.1        2014-03-27 CRAN (R 3.0.2)              
 stringr      0.6.2      2012-12-06 CRAN (R 3.0.2)     
harishnreddy commented 9 years ago

To be clear, I am using the code that I copied at the top and before starting the code do 'Restart R' in RStudio

harishnreddy commented 9 years ago

I also tried running this script outside of RStudio on my Mac (Rscript ) and ended up with the same response.

Was thinking there may be some contention between httr libraries in RStudio so tried to run it outside of RStudio.

hadley commented 9 years ago

You're using an old version of R6. If you upgrade the problem should go away

harishnreddy commented 9 years ago

Thanks. This got me much closer.

Am now fighting the TokenLinkedIn$new function.

Get the following error

> token <- TokenLinkedIn$new(endpoint = oauth_endpoints("linkedin"), app = myapp)
Use a local file to cache OAuth access credentials between R sessions?
1: Yes
2: No

Selection: 2
Error in !use_oob : invalid argument type
> 
> traceback()
4: init_oauth2.0(self$endpoint, self$app, scope = self$params$scope, 
       type = self$params$type, use_oob = self$params$use_oob)
3: self$init_credentials()
2: public_bind_env$initialize(...)
1: TokenLinkedIn$new(endpoint = oauth_endpoints("linkedin"), app = myapp)
>