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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

System.InvalidOperationException: Misused header name when adding Authorization

(0) ShareShare
ReportReport
Posted on by 8

I am trying to write AL code in a CodeUnit to get an OAuth token from my Auth0 service.  I can see the failed attempts in the Auth0 logs.  I realized I need to add the Base64 encrypted credentials to the request header to make it work.  So I added that code in and am getting the following error:

System.InvalidOperationException: Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

The error happens when I add this code:

RequestHeader.Remove('Authorization');
RequestHeader.Add('Authorization', AuthString);

And here is the relevant portion of my CodeUnit:

procedure getAuthToken(ClientSecret: Text; ClientId: Text; RedirectURL: Text; AuthURL: Text)
    var
        AuthPayload: Text;
        AuthString: Text;
        ResponseText: Text;
        TokenResponseText: Text;
        JObjectResult: JsonObject;
        JObjectRequest:  JsonObject;
        WebClient: HttpClient;
        RequestHeader: HttpHeaders;
        ResponseMessage: HttpResponseMessage;
        RequestMessage: HttpRequestMessage;
        RequestContent: HttpContent;
        TokenOutStream: OutStream;
        TokenExpiry: DateTime;
        Base64Convert: Codeunit "Base64 Convert";
        
    begin
    //Create webservice call
    RequestMessage.Method := 'POST';
    RequestMessage.SetRequestUri(AuthURL);

    //Create webservice header
    AuthString := STRSUBSTNO('%1:%2', ClientId, ClientSecret);
    AuthString := Base64Convert.ToBase64(AuthString);
    AuthString := STRSUBSTNO('Basic %1', AuthString);

    //Payload needed? This might as well be a different implementation!
    //It's just an example where the credentials are stored as a json payload

    //Create json payload
    JObjectRequest.Add('grant_type', 'client_credentials');
    // JObjectRequest.Add('client_id', ClientId);
    // JObjectRequest.Add('client_secret', ClientSecret);
    JObjectRequest.Add('audience', 'https://xxxx/');
    JObjectRequest.Add('scope', 'xxxx');
    JObjectRequest.WriteTo(AuthPayload);

    //Get Request Content
    RequestContent.WriteFrom(AuthPayload);

    //RequestHeader.Clear();
    RequestContent.GetHeaders(RequestHeader);
    RequestHeader.Remove('Content-Type');
    RequestHeader.Add('Content-Type', 'application/x-www-form-urlencoded');
    RequestHeader.Remove('Authorization');
    RequestHeader.Add('Authorization', AuthString);

    RequestMessage.Content := RequestContent;

I have the same question (0)
  • Suggested answer
    Nitin Verma Profile Picture
    21,794 Moderator on at

    Hi,

    Please refer the below link

    www.navwithnav.com/.../

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,005 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,148 Super User 2026 Season 1

#3
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 557 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans