Generating OAuth tokens from RServer using Google Analytics


#1

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:

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


#2

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.


#3

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