title: What is the correct way of obtaining an access token for service principal object?
body: <p>I have an API which is registered as a service principal in Azure AD. I'm trying to obtain an access token from a client application and pass that to the API.</p>
  <p>As part of the HttpClient request I've added this code (from Microsoft docs):</p>
  <pre><code>var accessToken = await tokenCredential.GetTokenAsync(
      new TokenRequestContext(scopes: new string[] { ResourceId + &quot;/.default&quot; }) { }
  );
  </code></pre>
  <p>This works, however it will only work for 24 hours, at which point I have to restart the app services to make it work again. I'm guessing this is because the token has expired and has not retrieved a new one.</p>
  <p>I've read somewhere that if the above code is used, expiration and cache for the token needs to be handled manually.</p>
  <p>How should I be retrieving these tokens using <code>Azure.Identity</code>?</p>