This document hopefully helps troubleshoot authentication troubles. If anything is not covered, please raise an issue on GitHub.
Generally, successful authentication needs the following in place:
A lot of the time the first two are set by the package and the end user does not need to worry about them. They are set via these options:
Once authentication has been done, then the authentication details are kept in a cache file, which by default is called
.httr-oauth and appears in your working directory.
This file location is set via
option("googleAuthR.httr_oauth_cache"), or when you pass a filename to
gar_auth(token = "filename") it will set it to the
filename you pass in.
At the same time a token is written to the R session into a global object, for use in the functions. On any restart of the R session, or if the token expires (default 60 mins) the cache file is looked for to reload it into the R session.
Several libraries wrap
googleAuthR::gar_auth() with presets, so you don’t call them directly e.g.
googleAuthR::gar_auth() instead of native authentication function if you want more control.
If you call
googleAuthR::gar_auth() then it will look for a cache token given by
option("googleAuthR.httr_oauth_cache") or the filename you pass in - if not present it will trigger the OAuth2 flow.
If you call
googleAuthR::gar_auth("token_name") then it will look for a cache token given by
"token_name" and set the
"token_name". This means refreshes etc. will be to that token cache file. It will also set the client ID, client secret and scopes to that specified in the file.
If for any reason any of the above change inbetween you authenticating and calling the API, then you may get an error or need to reauthenticate.
option("googleAuthR.client_secret")are changed by loading another library or setting them in a script, and then a cache file is used with different options set.
gar_auth_service()make sure the service email is an authenticated user with the service you are trying to access, and your Google Project has the right API turned on.
Error in init_oauth_service_account(self$secrets, scope = self$params$scope, : Bad Request (HTTP 400).then your auth request is probably malformed. Check you have set the scopes and clientID/secret correctly.
googleAuthR::gar_token_info(2)to check your current token, and validate it with
googleAuthR::gar_check_existing_token()which should return
TRUE. Make sure it matches your expectations.
library(googleAnalyticsR) options(googleAuthR.client_id = "XXXX", googleAuthR.client_secret = "XXXX") ## wraps `googleAuthR::gar_auth()` ga_auth()
GAR_AUTHor package supported ones.