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

gm_auth_configure() failing to auth with custom app provided via htrr::oauth_app() #144

Closed
Niekuba opened this issue Sep 21, 2020 · 15 comments · Fixed by #165
Closed

gm_auth_configure() failing to auth with custom app provided via htrr::oauth_app() #144

Niekuba opened this issue Sep 21, 2020 · 15 comments · Fixed by #165

Comments

@Niekuba
Copy link

Niekuba commented Sep 21, 2020

Right now, while attempting to run first auth using custom app setup (see code below), gm_auth_configure() returns error requesting either for path or key and secret parameters.

Not 100% sure if this is an error or just inconsistency with function documetation which uses code like below as an example. However, with other gargle-based apps, similar code works like charm

Code:

google_app <- httr::oauth_app(
    "my_google_app",
    key = "",
    secret = ""
)

gmailr::gm_auth_configure(app = google_app)

Error returned:

Error: Must supply either `key` and `secret` or `path`

My sessionInfo():

R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 10 (buster)

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.5.so

locale:
 [1] LC_CTYPE=C.UTF-8       LC_NUMERIC=C           LC_TIME=C.UTF-8        LC_COLLATE=C.UTF-8     LC_MONETARY=C.UTF-8   
 [6] LC_MESSAGES=C.UTF-8    LC_PAPER=C.UTF-8       LC_NAME=C              LC_ADDRESS=C           LC_TELEPHONE=C        
[11] LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C   

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] gmailr_1.0.0.9000 RMariaDB_1.0.9   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5      packrat_0.5.0   R6_2.4.1        lifecycle_0.2.0 DBI_1.1.0       magrittr_1.5    httr_1.4.2      rlang_0.4.7    
 [9] vctrs_0.3.4     tools_4.0.2     bit64_4.0.2     bit_4.0.4       hms_0.5.3       compiler_4.0.2  pkgconfig_2.0.3 base64enc_0.1-3
@hannahflaherty
Copy link

Not sure if my issue is related, but I am also having trouble authorizing an app. I followed instructions here to create my app and credentials, and downloaded a json file. But when I reference the file with gm_auth_configure(path = "my_file_path.json"), I get the error Error: Can't find 'client_id' and 'client_secret' in the JSON, which seems to actually be from the gargle::oauth_app_from_json function.

My json file is formed like this:

{
  "type": "service_account",
  "project_id": "project-id",
  "private_key_id": "",
  "private_key": "-----BEGIN PRIVATE KEY-----\n[redacted]\n-----END PRIVATE KEY-----\n",
  "client_email": "[email protected]",
  "client_id": "client_id",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://oauth2.googleapis.com/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/my-app.iam.gserviceaccount.com"
}

There is no value called "client_secret" or similar. Is it possible something has changed, or am I creating my app the wrong way?

@jademare
Copy link

jademare commented Dec 10, 2020

+1 @hannahflaherty, also, when I try with the OAuth JSON file, pasted below, I get the same error: Error: Can't find 'client_id' and 'client_secret' in the JSON.

Are we getting the right JSON file? Or we need to pass another argument to gm_auth_configure() rather than path?

{
   "web":{
      "client_id":"client_id",
      "project_id":"project_id",
      "auth_uri":"https://accounts.google.com/o/oauth2/auth",
      "token_uri":"https://oauth2.googleapis.com/token",
      "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
      "client_secret":"[redacted]",
      "redirect_uris":[
         "http://localhost:1410/"
      ]
   }

@alex-chisholm
Copy link

Hi @jeanprado were you able to solve this?

I'm also getting the Error: Can't find 'client_id' and 'client_secret' in the JSON even through both the client_id and client_secret are in the credentials.json file.

@astridite
Copy link

astridite commented Jul 15, 2021

Getting the same error when using gm_auth_configure(path="my.json") - I can see my client_id and client_secret n the .json.... but gmailR is choosing violence :P

@Steviey
Copy link

Steviey commented Sep 27, 2021

+1

@tomatoes-prog
Copy link

Same, I think the package is build to receive an old json structure

@yogat3ch

This comment has been minimized.

@jennybc
Copy link
Member

jennybc commented Dec 2, 2021

I just became maintainer of gmailr a couple of days ago, so it's my first time reading this thread. I can't truly work on gmailr right now, but I'll try to at least bring some clarity here today.

@yogat3ch
Copy link

yogat3ch commented Dec 2, 2021

Would one say Congratulations about that? I imagine it probably means more on an already full plate though!
I look forward to hearing what you think the issue might be. With enough detail I may be able to figure it out and PR

@jennybc
Copy link
Member

jennybc commented Dec 2, 2021

There are few different problems mixed together in this thread. I'm going to work through a few of them.

@hannahflaherty The JSON you show is for a service account, but you say you want to "authoriz[e] an app". This is a pretty common mix up, i.e. to have the JSON for a service account, but then present it to gargle as an OAuth client (or vice versa). This fails, for obvious reasons. You need to get an OAuth 2.0 client ID and secret and gargle's instruction for this are here: https://gargle.r-lib.org/articles/get-api-credentials.html.

@jennybc
Copy link
Member

jennybc commented Dec 2, 2021

@jeanprado The JSON you show indicates you've gotten an OAuth client for use in web applications (see the "web" key in the JSON). gmailr (gargle, really) cannot use such a client. The instructions for how to obtain an OAuth client for gmailr/gargle are here: https://gargle.r-lib.org/articles/get-api-credentials.html

The application type should be “Desktop app”.

google-oauth-client-desktop-app

@jennybc
Copy link
Member

jennybc commented Dec 2, 2021

Updating with more details: For gargle < 1.0.0, the JSON for a web application OAuth client would be rejected by oauth_app_from_json() (the error seen by @hannahflaherty @jeanprado, for sure, in terms of when they posted here).

Error: Can't find 'client_id' and 'client_secret' in the JSON

As of gargle >= 1.0.0 (released 2021-03-03), the JSON for a web application will be accepted, because it is useful to other packages (e.g. googleAuthR) and for some development experiments in gargle. But note that gargle itself does not really offer proper support (yet) for a true web app auth flow. In particular, gmailr and its use of gargle expect a "Desktop app" OAuth client.

Here is where this error originates, which explains why gargle will not see a client_id or client_secret if the overall OAuth client type is wrong / unexpected:

https://github.com/r-lib/gargle/blob/5f8fb44b5de7d0120e5a48e58fe8d89c7fbb6bc6/R/oauth-app.R#L24-L30

For those of you seeing Error: Can't find 'client_id' and 'client_secret' in the JSON after 2021-03-03 (@alex-chisholm, @astridite, @Steviey, @tomatoes-prog) either you're using old version of gargle or you've selected yet another (unsupported) application type, i.e. the JSON key is something other than "installed" or "web".

In short, many of the problems reported here (although ironically not what the original poster is reporting) come from having an unexpected OAuth client type.

@jennybc
Copy link
Member

jennybc commented Dec 2, 2021

@yogat3ch This thread is (mostly) not about service accounts, so I'm not going to follow up on that here and that is why I'm marking such comments as off-topic.

@Niekuba
Copy link
Author

Niekuba commented Dec 2, 2021

So just to update on the original topic:
I have checked that issue again with both newest R version as well as current gmailr github version (it seems this one haven't changed since then though) and the issue is still here.

While using the syntax from OP like following:

google_app <- httr::oauth_app(
    "my_google_app",
    key = "",
    secret = ""
)

gmailr::gm_auth_configure(app = google_app)

doesn't work. If you pass additional arguments (key, secret) into gm_auth_configure call, everything works fine.

Example of working code (note: values of "key" and "secret" in both oauth_app and gm_auth_configure are identical):

google_app <- httr::oauth_app(
    "my_google_app",
    key = "XXX",
    secret = "YYY"
)

gmailr::gm_auth_configure(app = google_app, key = "XXX", secret = "YYY")

@jennybc
Copy link
Member

jennybc commented Dec 2, 2021

Yeah, now that I've addressed all the off-topic stuff, I can return the original problem report, which doesn't look too difficult to fix.

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 a pull request may close this issue.

9 participants