web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

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

(0) ShareShare
ReportReport
Posted on by 3,512

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);
                    context.ExecuteQuery();
                    Console.WriteLine(context.Web.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);
	context.ExecuteQuery();
	Console.WriteLine(cc.Web.Title);
 }


I referred below articles:


docs.microsoft.com/.../security-apponly-azureacs
www.c-sharpcorner.com/.../

I have the same question (0)
  • Suggested answer
    PerezAguiar Profile Picture
    Microsoft Employee on at

    Hey Maulik.

    I think this is an incorrect forum for this question: This is the Dynamics Forum. In order to get a better response you should create your question on techcommunity.microsoft.com/.../SharePoint

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans