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,672 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. Gaurav Goyal 1 Reputation point
    2020-11-18T06:18:19.917+00:00

    I am also facing the same issue with new tenant. This is a serious issue with newly created tenant.
    The solution is only the command :

     Set-SPOTenant -DisableCustomAppAuthentication $false
    

    Thanks,
    Gaurav Goyal

    0 comments No comments

  2. NigelH 11 Reputation points
    2021-05-20T10:33:13.07+00:00

    I've been experiencing the same issue since 22/04, however it's on a tenant that has existed for a couple of years.

    0 comments No comments

  3. barry bijoy 26 Reputation points
    2021-05-25T10:59:43.023+00:00

    I was using Azure app service to host my app and facing this issue, Adding following line of code in the startup.Auth.cs in App_start folder fixed my issue

    ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;


  4. Hicham BOUCHAOUI 1 Reputation point
    2021-05-25T18:08:31.777+00:00

    Good morning all ,
    I need your help
    I tried to upload files to a SharePoint online site via an application

    Protocol Commands: >>POST https://xxxxxx.sharepoint.com/sites/Transfert_secure/Documents/_api/web/GetFolderByServerRelativeUrl('/sites/Transfert_secure/Documents/Targetfolder')/Files/Add(url='file.txt',overwrite=true) HTTP/1.1

    <<HTTP/1.1 401 Unauthorized

    I believe that the site should be granted permission through the AD application but I don't know how I could do it ?

    Thank you

    0 comments No comments