title: How to refresh Google OAuth 2 Access Token Kotlin
body: <p>I am trying to build an application that interacts with Google's API to pull data from the YouTube Data API. I have been able to get an access token and a refresh token by having the user authenticate manually but I want the program to run without user input. My current approach is to get a new access token each time the application run but this seems to, after some time, revoke the current refresh token for the user which I used to get a new access token. My code is below. Does anyone have any suggestions for what I am doing wrong or a better way to do this? Thanks</p>
  <pre><code>val scopes: ArrayList&lt;String&gt; = ArrayList()
              scopes.add(&quot;https://www.googleapis.com/auth/youtube.readonly&quot;)
              val tokenResponse: TokenResponse = GoogleRefreshTokenRequest(NetHttpTransport(), GsonFactory(), refreshToken, clientID, clientSecret)
                  .setScopes(scopes).setGrantType(&quot;refresh_token&quot;).execute()
              tokenResponse.accessToken
              val cred: GoogleCredential = GoogleCredential.Builder().setTransport(NetHttpTransport()).setJsonFactory(GsonFactory()).setClientSecrets(clientID, clientSecret).build().setAccessToken(tokenResponse.accessToken).setRefreshToken(tokenResponse.refreshToken)
  </code></pre>