SharePoint App-Only Add-ins throwing 401 Unauthorized on newly created O365 tenants

Paulius Baltrėnas 261 Reputation points
2020-09-09T11:47:00.883+00:00

Hi,

we have noticed that our SharePoint AddIn cannot get permissions on a newly created trial O365 tenant.

While getting the ClientContext with ClientID and ClientSecret we get this error "The remote server returned an error: (401) Unauthorized."

We have tried to register a new app-only principal to test if it works on a new tenant by following this documentation from Microsoft:

https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
After registering and trying again, on the new tenant we got the exact same error "The remote server returned an error: (401) Unauthorized."

But when we tried on an older tenant that we had, it worked fine for both our SharePoint Add-In and for a newly registered principal.

Very simple call using OfficeDevPnP nuget.

OfficeDevPnP.Core.AuthenticationManager am = new OfficeDevPnP.Core.AuthenticationManager();
using (Microsoft.SharePoint.Client.ClientContext context = am.GetAppOnlyAuthenticatedContext(createEntity.AppUrl, clientId, clientSecret))
{
Web web = context.Web;
context.Load(web, w => w.Id, w => w.Title);
context.ExecuteQueryRetry();
}

Is anyone else having the same issue on fresh newly created O365 tenants?

Or maybe there is some new setting to allow using "SharePoint App-Only" authentication?

I have posted the same question to another forum, but was redirected to post here also.
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_sharepoint-mso_win10-mso_o365b/sharepoint-app-only-add-ins-throwing-401/962bfaa2-8604-4e94-ae1c-36ef5b453ed2?tm=1599640808879

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,610 questions
{count} votes

Accepted answer
  1. Amos Wu-MSFT 4,051 Reputation points
    2020-09-10T09:35:56.287+00:00

    I would suggest you to create a service request in admin center,so our engineers could help you check this issue.

    ---------------------------------
    Updated---------------------------
    You could try to run below command:

    Set-SPOTenant -DisableCustomAppAuthentication $false  
    

    Tip:You need to update the SharePoint Online managed shell to the latest version.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.

11 additional answers

Sort by: Most helpful
  1. Hicham BOUCHAOUI 1 Reputation point
    2021-05-26T07:11:38.343+00:00

    Hello,
    Attached are the permissions of the Microsoft AD application

    99734-image.png

    Thank you

    0 comments No comments

  2. terryr 1 Reputation point
    2021-05-28T10:36:57.16+00:00

    Can anyone outline what configuration changes are needed in the Add-in to get it set up with my new Azure Active Directory Application? I can see the application in Enterprise applications but I can't get my Add-in to install when I use it's applicationId as the clientId in the Add-in config. The install just times out when I try to upload it to my dev site.

    0 comments No comments

  3. Amit Tare 1 Reputation point
    2021-10-01T10:06:42.737+00:00

    I am having the same issue I am SharePoint App Only Authentication. Did anyone get any solution.

    0 comments No comments