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

clear token on oauth2.0 error #315

Merged
merged 8 commits into from
Jan 8, 2016

Conversation

nathangoulding
Copy link
Contributor

This implements OAuth2.0 error handling as described in the RFC on refresh, and clears the token locally.

Fixes #311. BigQuery respects the OAuth2.0 RFC, so the behavior he describes is resolved without any changes to bigrquery.

Tested locally both using the local file cache and not.

@krlmlr
Copy link
Member

krlmlr commented Jan 6, 2016

This works better. On the first try, I'm seeing:

> query_exec(sql, project = project)
Auto-refreshing stale OAuth token.
No encoding supplied: defaulting to UTF-8.
Error: Invalid Credentials
In addition: Warning message:
In req$auth_token$refresh() :
  Unable to get refreshed credentials with refresh token
9: stop(out$err$message, call. = FALSE) at request.r#67
8: process_request(req) at request.r#34
7: bq_post(url, body) at jobs.r#60
6: insert_query_job(query, project, destination_table = destination_table, 
       default_dataset = default_dataset) at query.r#32
5: query_exec(sql, project = project) at oauth.R#4
4: eval(expr, envir, enclos)
3: eval(ei, envir)
2: withVisible(eval(ei, envir))
1: source("oauth.R")

Only the second try opens a browser for re-authentication. Furthermore, from RStudio, I was able to connect even after I had revoked the access from the Google console and after restarting RStudio. Then I tried from the R console, which led to the behavior above. I'm not sure if this is due to a timeout or some magic cache.

has_oauth2.0_error <- function(response) {
if (status_code(response) %in% oauth2.0_error_codes) {
content <- content(response)
if (content["error"] %in% oauth2.0_errors) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be content$error or content[["error"]]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it seems like it would be safer to assume that any error implies that it failed.

@@ -211,7 +211,12 @@ Token2.0 <- R6::R6Class("Token2.0", inherit = Token, list(
refresh = function() {
self$credentials <- refresh_oauth2.0(self$endpoint, self$app,
self$credentials, self$params$user_params)
self$cache()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logic here could be made more clear. Maybe:

creds <- refresh_oauth2.0(self$endpoint, self$app,
  self$credentials, self$params$user_params)
if (is.null(creds) {
  remove_cached_token(self)
  warning("Unable refresh token", call. = FALSE)
} else {
  self$credentials <- creds
  self$cache()
}
self

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe the warning should occur in refresh_oauth2.0()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think the warning should probably occur in refresh_oauth2.0 in thinking about it.

@nathangoulding
Copy link
Contributor Author

PTAL

@krlmlr
Copy link
Member

krlmlr commented Jan 8, 2016

I'm now reliably seeing the following behavior (using nathangoulding/bigrquery#71):

  • Initial state: Connection from bigrquery works, no service token set

  • Revoke access via https://myaccount.google.com/security#connectedapps

    • I can still connect from bigrquery for a minute or so
  • Eventually, I'm seeing the following as a result from query_exec():

    Auto-refreshing stale OAuth token.
    No encoding supplied: defaulting to UTF-8.
    Error: Invalid Credentials
    In addition: Warning message:
    Unable to refresh token 
    9: stop(out$err$message, call. = FALSE) at request.r#67
    8: process_request(req) at request.r#34
    7: bq_post(url, body) at jobs.r#60
    6: insert_query_job(query, project, destination_table = destination_table, 
           default_dataset = default_dataset) at query.r#32
    5: query_exec(sql, project = project) at oauth.R#5
    4: eval(expr, envir, enclos)
    3: eval(ei, envir)
    2: withVisible(eval(ei, envir))
    1: source("~/git/R/bigrquery/oauth.R", echo = TRUE)
    
  • The second call of query_exec() finally opens the authentication page in the browser

@hadley
Copy link
Member

hadley commented Jan 8, 2016

@nathangoulding LTGM - can you please add a bullet to NEWS?

@hadley
Copy link
Member

hadley commented Jan 8, 2016

@krlmlr I think eliminating the second refresh will require a tweak in bigrquery

@nathangoulding
Copy link
Contributor Author

Updated NEWS.

hadley added a commit that referenced this pull request Jan 8, 2016
@hadley hadley merged commit a653c52 into r-lib:master Jan 8, 2016
@hadley
Copy link
Member

hadley commented Jan 8, 2016

Thanks!

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

Successfully merging this pull request may close these issues.

Error after manually revoking OAuth permissions
3 participants