0
votes

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.

1

1 Answers

2
votes

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.