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

  • BringYourOwnDeveloper Profile Picture
    55 on at
    RE: Auth via bearer token from D365

    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);
        }

  • BringYourOwnDeveloper Profile Picture
    55 on at
    RE: Auth via bearer token from D365

    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

  • Suggested answer
    ergun sahin Profile Picture
    8,816 Moderator on at
    RE: Auth via bearer token from D365

    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");

  • Suggested answer
    Martin Dráb Profile Picture
    232,011 Most Valuable Professional on at
    RE: Auth via bearer token from D365

    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.

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

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,278 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,011 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans