MarkEdmondson1234 / googleAuthR

Google API Client Library for R. Easy authentication and help to build Google API R libraries with OAuth2. Shiny compatible.
https://code.markedmondson.me/googleAuthR
Other
175 stars 51 forks source link

Make authentication smoother on GCP R notebooks #147

Open MarkEdmondson1234 opened 5 years ago

MarkEdmondson1234 commented 5 years ago

There should be a way to detect if you are using via a Jupyter notebook, and auto-authenticate using gar_gce_auth() or similar. Also need to look at how non-cloud APIs like googleAnalyticsR best authenticate in notebooks as you can't use ga_auth()

MarkEdmondson1234 commented 5 years ago

Non-cloud APIs can either upload an offline token, or use gcloud to issue a user token:

## in the terminal, issue this gcloud command specifying the scopes to authenticate with
gcloud auth application-default login --scopes=https://www.googleapis.com/auth/analytics.readonly

## access the URL, login and create a verification code, paste in console.

## view then copy-paste the access token, to be passed into the R function
gcloud auth application-default print-access-token
# ya29.GlsiB-p2XXXXzuipzlNa_tZfqGoWpyZCNqf3-pWaioy-2mGjnrRos22acXXXX_94LUBjohSmboc-15saPfXXXXphhIj4mj9hT

Then in R, use the new function gar_gce_auth_defult to turn it into the expected R token:

gar_gce_auth_default(<token-copy-pasted>, 
                      scopes = 'https://www.googleapis.com/auth/analytics.readonly',
                      cache_file = 'my_ga.auth')

# use token to authenticate as you would normally with library
# set up auto-auth with googleAnalyticsR
Sys.setenv("GA_AUTH_FILE" = "my_ga.auth")

library(googleAnalyticsR)
#Successfully auto-authenticated via ./my_ga.auth