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

Community site session details

Session Id :
Small and medium business | Business Central, N...
Unanswered

SharePoint OAuth Token 401 Unauthorized from Business Central (Postman Works)

(1) ShareShare
ReportReport
Posted on by 151

We encountered a 401 Unauthorized error while generating a SharePoint OAuth access token from Business Central AL code, even though the same credentials and request worked perfectly in Postman.
In Postman, we made a POST request to:

https://accounts.accesscontrol.windows.net/<tenetnt id>/tokens/OAuth/2

with the following parameters are dummy but we have used the correct ones:

Body : x-www-form-urlencoded

  • client_id: abcd1234-5678-90ef-ghij-klmnopqrstuv@12345678-aaaa-bbbb-cccc-1234567890ab
  • client_secret: XyZ6OFF+TUE0TVFCWk00fnFXNUVpQTJPc0NhLlU1TWdybTBYV2FJVA==

  • resource: 00000003-0000-0ff1-ce00-000000000000/mysharepointsite.sharepoint.com@12345678-aaaa-bbbb-cccc-1234567890ab

  • grant_type: client_credentials

Header :

  • Content-Type  : - application/x-www-form-urlencoded
The access token was generated successfully, and we were even able to upload files to SharePoint directly from Postman.

Business central
However, when we tried to replicate the same process from Business Central AL code, using the same credentials and request parameters, the request failed with a 401 Unauthorized error. Below is the AL code we used (with dummy credentials):
 
procedure GetOauthToken()
var
    Request: HttpRequestMessage;
    Response: HttpResponseMessage;
    Content: HttpContent;
    ContentHeaders: HttpHeaders;
    ContentText: Text;
    GetTokenAuthorization: JsonObject;
    Token: Text;
    Client: HttpClient;
begin
    ContentText := 'grant_type=client_credentials' +
                   '&client_id=abcd1234-5678-90ef-ghij-klmnopqrstuv@12345678-aaaa-bbbb-cccc-1234567890ab' +
                   '&client_secret=XyZ6OFF+TUE0TVFCWk00fnFXNUVpQTJPc0NhLlU1TWdybTBYV2FJVA==' +
                   '&resource=00000003-0000-0ff1-ce00-000000000000/mysharepointsite.sharepoint.com@12345678-aaaa-bbbb-cccc-1234567890ab';
    Content.WriteFrom(ContentText);
    Content.GetHeaders(ContentHeaders);
    ContentHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');
    Request.Method := 'POST';
    Request.SetRequestUri('https://accounts.accesscontrol.windows.net/12345678-aaaa-bbbb-cccc-1234567890ab/tokens/OAuth/2');
    Request.Content(Content);
    if Client.Send(Request, Response) then
        if Response.IsSuccessStatusCode() then begin
            Response.Content.ReadAs(ContentText);
            GetTokenAuthorization.ReadFrom(ContentText);
            Token := GetTokenAuthorization.GetValue('access_token').AsText();
        end else
            Error('Unauthorized! Response code: %1', Response.HttpStatusCode());
end;
 
Do we need to configure something additionally in Azure App Registration or within Microsoft Entra ID (formerly Azure AD) or the Business Central Microsoft Entra application for this to authenticate correctly?

If anyone from the community has encountered this scenario or resolved it before, your guidance would be really appreciated.
I have the same question (1)

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 2,828

#2
Sumit Singh Profile Picture

Sumit Singh 2,617

#3
YUN ZHU Profile Picture

YUN ZHU 1,861 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans