knitr::opts_chunk$set( collapse = TRUE, echo = FALSE, eval = TRUE, fig.align = "center", comment = "#>" )
To upload, edit, and download our files in Google Drive, trackdown relies on the Google APIs. Using Google APIs requires two main things:
In the next sections, we describe all the steps to configure everything is needed to use trackdown.
Trackdown already provides an internal client app to use the Google APIs. However, the numebr of users is limited and we already reached the maximum number (i.e., 100 users). Thus to use trackdown, we are required to create our own Client App in Google Cloud Console.
To create a Client App in Google Cloud Console, follow these steps:
knitr::include_graphics('images/01-new-proj.png')
Specify the name of the project and click "CREATE"
knitr::include_graphics('images/02-proj-name.png')
Select the newly created project to open it
knitr::include_graphics('images/03-proj-select.png')
knitr::include_graphics('images/04-api.png')
Click the "ENABLE APIS AND SERVICES" button
knitr::include_graphics('images/05-api.png')
Using the API Library search service we need to select two APIs
knitr::include_graphics('images/06-api-search.png')
Select "Google Drive API" from the list and click "ENABLE"
knitr::include_graphics('images/07-api-drive.png')
Select "Google Docs API" from the list and click "ENABLE"
knitr::include_graphics('images/08-api-docs.png')
knitr::include_graphics('images/09-consent-external.png')
Specify the App information "App name" and "User support email" (use your email)
knitr::include_graphics('images/10-consent-info.png')
Specify also the developer contact information (use your email) and press "SAVE AND CONTINUE"
knitr::include_graphics('images/11-consent-contact.png')
knitr::include_graphics('images/12-scopes.png')
Select the required scopes ".../auth/userinfo.email" and "openid"
knitr::include_graphics('images/13-scopes-list.png')
Select also ".../auth/drive" and ".../auth/docs"
knitr::include_graphics('images/14-scope-docs.png')
Click "Update" at the end of the list to save changes.
> After selecting all the required scopes, we should obtain the following summary list > ```r > knitr::include_graphics('images/15-scope-summary.png') > ``` > Note that we selected sensitive and restriced scopes. Do not worry, these scopes are required to managed files in Google Drive.
knitr::include_graphics('images/15-test-user.png')
knitr::include_graphics('images/16-publish-app.png')
> Do not worry about the message. It is just reporting the requirements for the verification of our app by Google. > > We created the app and published it so other users can use it. As our newly created app uses sensitive and restricted scopes (it uses the google docs API to upload/download files on users google drive), Google allows only a limited number of users (100). To be open to anyone, the app must be verified by Google. This requires all the points listed in the message. However, this is an extra step you do not need. > > If we are planning to use our new app with a limited number of users (less than 100). We do not need this verification. So we do not need to provide other information to Google.
knitr::include_graphics('images/17-credentials.png')
As "Application type", select "Desktop app" and specify the client app name. Next click "CREATE"
knitr::include_graphics('images/18-credentials-type.png')
A window will display the credentials to use the app. We need to download the JSON file with the credentials by clicking "DOWNLOAD JSON" or alternatively we can copy the "Client ID" and "Client Secret"
knitr::include_graphics('images/19-credentials-popout.png')
This information are required to use the Google APIs through our newly created app. Only users with this information can use our app. We can also retrieve the credentials in a second moment by clicking the download button from the list "OAuth2.0 Client IDs"
knitr::include_graphics('images/20-credentials-client.png')
Once created our client app and obtained the credentials to use it (i.e., the JSON file), we are ready to configure trackdown.
To configure trackdown, follow these steps:
TRACKDOWN_CLIENT
environment variable to the location of the JSON file. It is convenient to do this in our .Renviron
file with usethis::edit_r_environ()
. In this way, trackdown will be able to automatically retrieve the app credentials. Be careful of using the correct absolute/relative path according to the specific needstrackdown_auth_configure(path = "path/to/downloaded/json")
by indicating where the JSON lives. This command needs to be run at each session. r
my_client <- gargle::gargle_oauth_client(
name = "my-client-name",
id = "123456789.apps.googleusercontent.com",
secret = "abcdefghijklmnopqrstuvwxyz"
)
trackdown_auth_configure(client = my_client)
knitr::include_graphics('images/21-warning.png')
knitr::include_graphics('images/22-acccess.png')
Now, we are ready to use trackdown!
It is important that trackdown has access to our client app credentials.
By default, trackdown first checks if the path to the JSON file is specified via the TRACKDOWN_CLIENT
environment variable. If not, trackdown will use its internal credentials. However, we have already exceeded the user limits. So, user custom client app credentials need to be indicated specifying the JSON file path or the app info (obtained via gargle::gargle_oauth_client()
function) in the function trackdown_auth_configure()
.
If you see the following error is because you are using the trackdown internal credentials and we have already exceeded the user limits.
knitr::include_graphics('images/23-errror.png')
Note that the credential used by trackdown will be automaticlayy applied also to googledrive package functions used internally by trackdown functions
trackdown manage the authentication process using gargle R-package. By default, user credentials are cached in a folder in the user’s home directory, ~/.R/gargle/gargle-oauth
, from where they can be automatically refreshed, as necessary. Storage at the user level means that the same token can be used across multiple projects and tokens are less likely to be synced to the cloud by accident. Note that if one is using R from a web-based platform like RStudio Server or Cloud, there will be a variant of this user authentication flow, known as out-of-band auth (“oob”).
It is possible to personalize gargle’s default settings by adding options in the .Rprofile
startup file. For example, the preferred Google account and cache folder can be set:
options( gargle_oauth_email = "my_email@gmail.com", gargle_oauth_cache = "/path/to/folder/that/does/not/sync/to/cloud" )
For more details see:
To learn how to manage credential securely, see: - https://gargle.r-lib.org/articles/managing-tokens-securely.html
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.