title: SharePoint App-Only Authentication throwing "The remote server returned an error: (401) Unauthorized."
body: <p>I am trying to make sharepoint authentication using oAuth or App-Only authentication, but it gives error &quot;The remote server returned an error: (401) Unauthorized.&quot;</p>
  <p>I am using trial sharepoint tenant.</p>
  <p>Here is my code snippets:</p>
  <p><strong>Using Client id &amp; Client secret:</strong></p>
  <pre><code>string siteUrl = &quot;[Sharepoint-Site-URL]&quot;;
  
  //Sharepoint App details
  string SPClientId = @&quot;[Client-id]&quot;;
  string SPClientSecret = @&quot;[Client-secret]&quot;;
  using (ClientContext context = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, SPClientId, SPClientSecret))
   {
       context.Load(context.Web, t =&gt; t.Title);
       context.ExecuteQuery();
       Console.WriteLine(context.Web.Title);
   };
  </code></pre>
  <p><strong>Using AccessToken:</strong></p>
  <pre><code> string siteUrl = &quot;[Sharepoint-Site-URL]&quot;;
   string realm = TokenHelper.GetRealmFromTargetUrl(new Uri(siteUrl));
  
   //Get the access token for the URL.  
   string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, new Uri(siteUrl).Authority, realm).AccessToken;
  
   //Create a client context object based on the retrieved access token
   using (ClientContext context = TokenHelper.GetClientContextWithAccessToken(siteUrl, accessToken))
   {
      context.Load(cc.Web, t =&gt; t.Title);
      context.ExecuteQuery();
      Console.WriteLine(cc.Web.Title);
   }
  enter code here
  </code></pre>
  <p>I referred below articles:
  <a href="https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs</a>
  <a href="https://www.c-sharpcorner.com/article/connect-to-sharepoint-online-site-with-app-only-authentication/" rel="nofollow noreferrer">https://www.c-sharpcorner.com/article/connect-to-sharepoint-online-site-with-app-only-authentication/</a></p>