skardhamar / rga

R Google Analytics
186 stars 89 forks source link

Ability to refresh oauth token? #98

Open odonnelladam opened 6 years ago

odonnelladam commented 6 years ago

I have a script that runs automatically on a server every day. Unfortunately, any time that server gets rebooted the script crashes because there's no one there to paste in the authentication code. I've used other packages in the past that have some kind of oauth_token$refresh() function. Any way to do that with this package?

EricGoldsmith commented 6 years ago

Are you storing an instance on disk, per:

https://github.com/skardhamar/rga#advanced-use

odonnelladam commented 6 years ago

Yes. Or at least attempting to. I'm taking over code from a previous developer so perhaps it's not correct.

Here's what I have now. Should I be doing an open 3 times, or perhaps combine into one command? Do I need to fill in a specific file path after the "where" below, or does this default to the WD?

library(rga) rga.open(instance="ga") rga.open(instance="ga", where="~/ga.rga") rga.open(instance = "ga", client.id = "myclientid", client.secret = "myclientsecret")

EricGoldsmith commented 5 years ago

No, 3 calls are not needed. This is what I do:

rga.open(instance = "ga", where="~/ga.rga",
         client.id = getOption("ga.clientId"), client.secret = getOption("ga.clientSecret"))

The getOption() is retrieving credentials from ~/.Rprofile, in order to keep them out of the code.

Do I need to fill in a specific file path after the "where" below, or does this default to the WD?

The ~/ location refers to the home directory ($HOME) of the user running the script. So, not the current working directory (WD). Though, you could store it there if you wanted to.

odonnelladam commented 5 years ago

OK, makes sense. I've kept it as Home (~/ga.rga), but I'm getting the following error: "Error in if (.self$tokenExpiresIn() <= 0) { : argument is of length zero"

I'm just running the following:

library(rga) rga.open(instance = "ga", where="~/ga.rga", client.id = getOption("ga.clientId"), client.secret = getOption("ga.clientSecret"))

I've also tried running the following first, in case I needed to store the ID/CS first:

ga.clientId <- "[myclientidhere]" ga.clientSecret <- "[mygasecrethere]"

I get the same error each time.

I've also tried running just rga.open(instance="ga") and pasting in the code just in case I needed to have a successful instance opened at least once. When I do that the code you suggested works fine. But if I close R Studio and re-open, thereby ending the instance, I can't just run the code you suggested without getting that same "length zero" error.

What am I missing?

I really appreciate your help with this. I assuming I'm just missing something basic.

EricGoldsmith commented 5 years ago

When using the options mechanism in R, you need to assign them as follow (this is an excerpt from my ~/.Rprofile):

# Google Analytics credentials
options(ga.clientId = "[myclientidhere]")
options(ga.clientSecret = "[mygasecrethere]")

And, remember that every time you modify .Rprofile you need to restart R for it to pick up the changes.

odonnelladam commented 5 years ago

Damn, I had already done that. Still getting that error. Not sure it's related to the GetOPtion, though. Because I still get the error even if I just declare it in the code such as

rga.open(instance = "ga", where="~/ga.rga", client.id = "[myclientidhere]", client.secret = "[mygasecrethere]")

I definitely intend to use the GetOption, though. Just doesn't seem to impact the error.

EricGoldsmith commented 5 years ago

Maybe the ~/ga.rga instance file is corrupt? Maybe try deleting it, then re-running the rga.open() call.

There's a status function that might provide some insight: ga$status(). The output for me looks like:

> ga$status()
Client ID: [myclientidhere]
Client Secret: [mygasecrethere]
Where: ~/ga.rga
Is token valid: TRUE
Token expires in: 3584.93204188347 seconds
odonnelladam commented 5 years ago

Good call. That's exactly what I did and it seems to be working. I noticed that the timestamp for the ga.rga file wasn't updating even when I authenticated. So I deleted the file, closed down everything and started over. This time when I ran the code it asked me to authenticate and created a new ga.rga file.

I was just testing it before I posted an update.

Thanks for your help with this!