Reputation: 31
A refresh token is not available when I follow Hadley's R google Oauth2.0 demo to access Fusion tables.
Demo: https://github.com/hadley/httr/blob/master/demo/oauth2-google.r
Example of modified "offline" attempt:
google_token <- oauth2.0_token(oauth_endpoints("google"), myapp,
scope = "https://www.googleapis.com/auth/fusiontables",
type= "offline",
use_oob = FALSE,
cache = TRUE)
Any direction on how to retrieve a refresh token is much appreciated.
UPDATE: Using the follow code a character string is returned with google_token$credentials. Is this the authorization code referenced here:https://developers.google.com/accounts/docs/OAuth2WebServer#offline
google_token <- oauth2.0_token(oauth_endpoints("google"), myapp,
scope = "https://www.googleapis.com/auth/fusiontables",
type= "access_type='offline'",
use_oob = FALSE,
cache = TRUE)
Thank you.
Upvotes: 1
Views: 1807
Reputation: 409
Even later to the party but chiming in here as I very much struggled to obtain a token object that does contain a refresh token.
Here's my code:
# get google client ID and secret from JSON file
google_client <- gargle::gargle_oauth_client_from_json(".secrets/myapp.json")
# create app and endpoint objects
app <- httr::oauth_app(appname = "myapp", key = google_client$id, secret = google_client$secret)
endpoint <- httr::oauth_endpoints("google")
# generate token to access google drive and google presentations (change the scopes to whatever you need)
token <- httr::oauth2.0_token(endpoint = endpoint, app = app,
scope = c("https://www.googleapis.com/auth/presentations",
"https://www.googleapis.com/auth/drive"),
cache = ".secrets/httr-oauth")
token$credentials
$access_token
[1] "XXX"
$expires_in
[1] 3599
$refresh_token
[1] "1//YYY"
$scope
[1] "https://www.googleapis.com/auth/presentations https://www.googleapis.com/auth/drive"
$token_type
[1] "Bearer"
Turns out my problem was not the code but the app I created in the google cloud. In order to receive a refresh token, you must select "Desktop app" when creating a OAuth2 client ID. I think this will automagically set access type to "offline". Download this desktop client as JSON (in my case .secrets/myapp.json
).
Upvotes: 1
Reputation: 21
I'm a bit late to the party here but hopefully this helps someone. I found this question last week because I was struggling with the same issue. Like you, I read the API documentation and tried the "offline" in the "type" field of the "oauth2.0_token()" function but it messed up the response. I downloaded the httr package source files from the github repository and had a look around. After some digging I found a way around it.
if you modify the "authorize-url" variable in oauth-init from this:
authorize_url <- modify_url(endpoint$authorize, query = compact(list(
client_id = app$key,
scope = scope_arg,
redirect_uri = redirect_uri,
response_type = "code",
state = state)))
to this:
authorize_url <- modify_url(endpoint$authorize, query = compact(list(
client_id = app$key,
scope = scope_arg,
redirect_uri = redirect_uri,
response_type = "code",
state = state,
access_type="offline")))
and then source the oauth-token and all the dependent functions (including oauth-init) you'll get a refresh token. For some reason, when oauth_token calls init_oauth2.0 it doesn't pass the "type" argument.
It's a nasty workaround and I've probably committed several sins but it does work. and you do get a refresh token.
Upvotes: 2