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 :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Auth via bearer token from D365

(0) ShareShare
ReportReport
Posted on by 55

Hello community,

I ran into following problem (D365 solution with .net Framework 4.6.2):

I want to consume from D365 F&X an external API Endpoint which needs a bearer token auth.

On several websites (MSDN, StackOverflow) the recomended way to provide the auth details is via DefaultRequestHeader or on the HttpRequestMessage (depending wether it changes per request or not). Neither ways are working, because (and this is my assumption) the DefaultRequestHeader is a property with only a getter.

I also tried to work with the add method without succes.

The error I get from our beloved compiler is: Error Invalid token '('.

using System.Net.Http;
class DefinitelyNotAClass
{
    public static void main(Args _args)
    {
        HttpClient httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "DefinitelyNotABearerToken");
    }
}

2021_2D00_09_2D00_21-17_5F00_37_5F00_49_2D00_Window.png

My question is: How should I provide the bearer token authentication from a D365 solution with the HttpClient?

Thanks in advance and best regards,

Oliver

I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,959 Most Valuable Professional on at

    DefaultRequestHeaders indeed can't be set, but that's not a problem at all, because we don't want to replace it. All we want to set is the value of its Authorization property, which is allowed.

  • Suggested answer
    ergun sahin Profile Picture
    8,826 Moderator on at

    Are you getting the error from the compiler or from the debugger? (For this code you need a valid token but in this case you get this error on runtime)

    If I'm right, the service you are trying to use has a method (process) that asks you for a user/password and gives you a token, or it accepts an empty string.

    Note: Your use of add is incorrect, it should be something like the following.

    httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer" "Your Oauth token");

  • BringYourOwnDeveloper Profile Picture
    55 on at

    Hello Martin and Ergün,

    I modified the code according to your suggestions, yet the compiletime error persists.

    I only compile both lines of code (158 & 159) at the same time for screnshot puposes; I tried to use this indenpedently.

    The code assumes correct references on class level (System.Net.Http).

    The bearer token 'replaceWithValidToken' is only a placeholder. In my concrete implementation there is a valid token included (tested & verfied via Postaman). The error happens at compile and not at runtime.

    private static void consumeEndpoint(DocuRef _docuRef, HttpRequestMessage _httpRequestMessage, str _userId = #AdminUser)
    {
        DocuType docuType = _docuRef.docuType();
        DocuValue docuValue = _docuRef.docuValue();
    
        str bearerToken = 'replaceWithValidToken';
        
        HttpClient httpClient = new HttpClient();
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken);
        httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer "   bearerToken);
    
        httpClient.SendAsync(_httpRequestMessage, HttpCompletionOption::ResponseContentRead);
    }

    2021_2D00_09_2D00_22-08_5F00_45_5F00_59_2D00_Window.png

    Any further suggestions?

    With kind regards,

    Oliver

  • BringYourOwnDeveloper Profile Picture
    55 on at

    I found a hint in this forum article. Martin, you also participated there (not suprisingly ).

    It seems like nested C# classes cannot be set with the syntax I used in the original post, although this is valid C# code. This seems to be a compiler error.

    I modified the X code accordng to the other forum article to fix it:

    private static void consumeEndpoint(DocuRef _docuRef, HttpRequestMessage _httpRequestMessage, str _userId = #AdminUser)
        {
            DocuType docuType = _docuRef.docuType();
            DocuValue docuValue = _docuRef.docuValue();
    
            str bearerToken = 'replaceWithValidToken';
            
            HttpClient httpClient = new HttpClient();
            Headers.HttpRequestHeaders defaultRequestHeaders = httpClient.DefaultRequestHeaders;
            defaultRequestHeaders.Authorization = new Headers.AuthenticationHeaderValue("Bearer", bearerToken);
    
            httpClient.SendAsync(_httpRequestMessage, HttpCompletionOption::ResponseContentRead);
        }

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 592 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 478 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 305 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans