Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring listener_endpoint in httr when using Rstudio server

I;'m struggling to connect to Google Analytics with httr oauth2.0 function

oauth2.0_token(oauth_endpoints("google")
  , oauth_app("google", client.id, client.secret)
  , scope = "https://www.googleapis.com/auth/analytics.readonly")

It works perfectly in my local Rstudio, but it breaks in AWS-based Rstudio Server. The error appears when I agree to pass data in browser and Google redirects me to the page http://localhost:1410/?state=codehere

When launching authentication in local Rstudio, browser responds with a message - Authentication complete. Please close this page and return to R, incase of Rstudio server it's just This webpage is not available

I suspect I need to change listener_endpoint configuration, but how? Should I put my Rstudio server address instead of default 127.0.0.1? Or is it flaw of httr+Rtudio server and I should not bother?

like image 271
RInatM Avatar asked Nov 09 '22 05:11

RInatM


1 Answers

Your redirect URI is part of the problem. Httr's oauth2.0_token() function identify the correct one. When you set up your project, Google Analytics created two redirect URIs, one that can be used on your RStudio IDE (local) and one that can be used in the RStudio web-based environment for out-of-of band authentication: "urn:ietf:wg:oauth:2.0:oob"

Once you've authenticated, the following code should work.

library(httr)
ga_id         <- YourProjectID
client_id     <- YourClientID
redirect_uri  <- 'urn:ietf:wg:oauth:2.0:oob'
scope         <- YourScope 
client_secret <- YourSecret
response_type <-'code'

auth1 <- oauth2.0_token(
   endpoint = oauth_endpoints("google"),
   app = oauth_app(
      "google", 
      key = client_id, 
      secret = client_secret
      ),
   scope,
   use_oob = TRUE,
   cache = TRUE
)

-- Ann

like image 63
A Nakamura Avatar answered Nov 15 '22 11:11

A Nakamura