Generating OAuth tokens from RServer using Google Analytics

I am having great difficulties trying to get R set up to pull in Google Analytics information. I have created the project in Google, credentials, enabled them, etc. etc. No problems from that end (at least I don't think so), but I cannot get anything to work within R. I've read that using R Server requires one to get past the "normal" authentication methods. I found this article located here:

https://support.rstudio.com/hc/en-us/articles/217952868-Generating-OAuth-tokens-for-a-server-using-httr

that addresses this (the example in this article using google sheets instead). I decided to use this code (as contained in the article):

options(httr_oob_default=TRUE)
gs_auth(new_user = TRUE)
gs_ls()

but when R runs gs_auth(new_user = TRUE)

I get the following error:

Error in gs_auth(new_user = TRUE) : could not find function "gs_auth"

Any thoughts on this? I have not been able to figure this despite repeated attempts and research. Thank you!

Greg

So gs_auth() is part of the googlesheets package. I’m guessing you haven’t loaded that package, so that’s the source of the error. I find the relevant paragraph in the linked support doc rather confusingly written myself, so I can see how you might have missed this detail!

Also, since the gs_auth() function is specifically written for interacting with Google Sheets, I don’t know if it will work to authenticate with Google Analytics or not. Unfortunately, authenticating with Google Analytics from R being run on a server is well beyond my experience, so I hope somebody else will turn up with a more comprehensive solution.

1 Like

Excellent catch! Okay, so I figure that the gs part of gs_auth is "googlesheets" (I missed that initially). I changed the gs to ga (for google analytics) and got a "success code" but when I go to paste that success code back into R , I see that R has the following error message:

options(httr_oob_default=TRUE)
ga_auth(new_user = TRUE)

2018-06-27 10:50:26> Removing old cached credentials from: /home/ONID.OREGONSTATE.EDU/simmongr/.httr-oauth
Enter authorization code: ga_ls()
Error in oauth2.0_access_token(endpoint, app, code = code, user_params = user_params, :
Bad Request (HTTP 400). Failed to get an access token.

That probably doesn't do much in the way of a solution but maybe I am mistaken. I have loaded all the relevant Google Analytics packages, so not sure what's wrong. Thank you!!!

G

1 Like

I am having the same issue and am getting an (HTTP 400) Request as well.
This is basically a forbidden code received from a mal-formed token from Google.
I am trying to solve the issue as well, but I know the problem is that I am not getting the token and caching it in the hidden .httr-oauth file.