SharePoint App-Only Authentication throwing "The remote server returned an error: (401) Unauthorized

Copper Contributor

Hi all,
I am trying to make sharepoint authentication using oAuth or App-Only authentication, but it gives error "The remote server returned an error: (401) Unauthorized.".

I am using trial sharepoint tenant.

Here is my code snippets:
Using Client id & Client secret:



string siteUrl = "[Sharepoint-Site-URL]";
                //Sharepoint App details
                string SPClientId = @"[Client-id]";
                string SPClientSecret = @"[Client-secret]";
                using (ClientContext context = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, SPClientId, SPClientSecret))
                    context.Load(context.Web, t => t.Title);



Using AccessToken:



string siteUrl = "[Sharepoint-Site-URL]";
 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 => t.Title);



I referred below articles:

1 Reply
Hello Maulik

Are you able to resolve above issue, if yes please can you please share the solution.