title: How to do YouTube OAuth Authentication/Reauthentication in R Automatically Using HTTR
body: <p>I've been using the HTTR package to make requests to the YouTube Data API. I have two channels that I am getting stats for. Upon the first use, when requesting an access token I did the whole OAuth dance where I was redirected to Google as expected. However, I saved the tokens in  <code>.httr-oauth</code> files. I developed a for loop similar to the following for me to get my statistics:</p>
  <pre><code>channelOneFile &lt;- &quot;.httr-oauth-channel1&quot;
  channelTwoFile &lt;- &quot;.httr-oauth-channel2&quot;
  
  myData &lt;- list()
  for(i in 1:2){
      token &lt;- try(suppressWarnings(readRDS(tokenFile)), silent = TRUE)[[1]]
      # And Then I get stats with a GET Request
      url &lt;- paste0(&quot;https://youtube.googleapis.com/youtube/v3/videospart=contentDetails%2Csnippet&quot;,
                    &quot;&amp;id=&quot;,videoId)
      request &lt;- GET(url, token)
      myData[[i]] &lt;- request
  }
  
  </code></pre>
  <p>which worked well for a while. However, after running this code roughly 20x or so (due to some testing and tweaking parameters, ONE of the get requests came back with</p>
  <pre><code>Auto-refreshing stale OAuth token.
  Warning: Unable to refresh token: invalid_grant
  Token has been expired or revoked.
  </code></pre>
  <p>At this point I'd just delete the &quot;.httr-oauth&quot; file, reauthenticate, save the new file, and then my code works again. However, I'm hoping to have this code automated on a local server, where the server (I think) can't do the OAuth dance if the token doesn't work.</p>
  <p>When I make a request, I know HTTR automatically uses the refresh token to get a new access token, and I know that Google APIs has a limit of how many times a refresh token can be used. I <em>think</em> that perhaps when it gets the new token it doesn't update the &quot;.httr-oauth&quot; file? How can I get it to do that? If that's not an option, what can I do to do the authentication ONCE, save the credentials and access token and such in a file, and then refer to that file when making server requests?</p>
  <p>UPDATE: Here is my OAuth flow:</p>
  <pre><code>if (file.exists(tokenFile)) {
     token &lt;- try(suppressWarnings(readRDS(tokenFile)), silent = TRUE)[[1]]
  } else if (is.null(appId) | is.null(appSecret)) {
      stop(&quot;Missing App Credentials&quot;)
  } else {
    token &lt;- httr::oauth2.0_token(httr::oauth_endpoints(&quot;google&quot;), 
                                  httr::oauth_app(&quot;google&quot;, appId, appSecret), 
                                  scope = c(&quot;https://www.googleapis.com/auth/youtube.readonly&quot;, 
                                                          &quot;https://www.googleapis.com/auth/yt-analytics.readonly&quot;))
  }
  </code></pre>