Troubleshooting Authentication

Mark Edmondson

2023-04-11

This document hopefully helps troubleshoot authentication troubles. If anything is not covered, please raise an issue on GitHub.

Helpful Resources

Setting your own client ID

Donal Phipps has a helpful video on how to set up your own Google Project client ID and secret via this link - its for googleAnalyticsR but applies for all googleAuthR packages.

Understanding authentication

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 options("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.

gar_auth() options

Several libraries wrap googleAuthR::gar_auth() with presets, so you don’t call them directly e.g. ga_auth() in googleAnalyticsR. Use 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 options("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 options("googleAuthR.httr_oauth_cache") to "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.

Common errors

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.

Tips and tricks

library(googleAnalyticsR)

options(googleAuthR.client_id = "XXXX", googleAuthR.client_secret = "XXXX")

## wraps `googleAuthR::gar_auth()`
ga_auth()

gar_auth("my_token.httr-oauth")