Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to refresh oauth token? #98

Open
odonnelladam opened this issue Oct 5, 2018 · 8 comments
Open

Ability to refresh oauth token? #98

odonnelladam opened this issue Oct 5, 2018 · 8 comments

Comments

@odonnelladam
Copy link

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
Copy link
Contributor

Are you storing an instance on disk, per:

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

@odonnelladam
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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
Copy link
Contributor

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
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants