For reference, I solved it with the following code:
options(gargle_oauth_cache = ".secrets")
gargle::gargle_oauth_cache()
gm_auth_configure(path = "credentials.json") # this json is obtained here: https://developers.google.com/gmail/api/quickstart/python
gm_auth(cache = gargle::gargle_oauth_cache(), use_oob = TRUE)
The above code needs to be run just once, and then every time using the code I should run:
gm_auth_configure(path = "credentials.json")
options(
gargle_oauth_cache = ".secrets",
gargle_oauth_email = TRUE
)
gm_labels() #to test that it works
Still it would be interesting to know if it is possible to use a different port