Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Dynamics 365 BC -SOAP service OAuth Authentication

Posted on by 60

Hello,

Today, I am using SOAP service to Integrate expense reports to Dynamics 365 BC Purchase Invoice.

I have used basic authentication like below

ICredentials credentials = new NetworkCredential(userName,password);

bcService.Credentials = credentials;

bcService.Create(ref purchaseInvoice);

Now I want to use OAuth Authentication while creating a purchase invoice.

Please suggest how can I use OAuth Authentication for the SOAP service to integrate data to Dynamics 365 BC.

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,565 Super User 2024 Season 2 on at
    RE: Dynamics 365 BC -SOAP service OAuth Authentication

    Hi, just adding Steven's reply.

    The capability of exposing SOAP endpoints will be removed in a later release.

    https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/upgrade/deprecated-features-platform#soap-endpoints-warning

    pastedimage1652142310017v1.png

    Hope this also helps.

    Thanks.

    ZHU

  • Steven Renders Profile Picture
    Steven Renders 5,051 Super User 2024 Season 1 on at
    RE: Dynamics 365 BC -SOAP service OAuth Authentication

    Suggestion: don't use SOAP, use the Rest API's instead.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Dynamics 365 BC -SOAP service OAuth Authentication

    Hi,

    Can you please re-use as per below?

     gbodyContent := StrSubstNo('access_token_url=%1&client_id=%2&client_secret=%3&grant_type=%4&scope=%5', AadTenantId, ClientIdTxt, ClientSecretTxt, 'client_credentials', ScopeId);

           gHttpcontent.WriteFrom(gbodyContent);

           gHttpcontent.GetHeaders(gHttpheaders);

           gHttpheaders.Remove('Content-Type');

           gHttpHeaders.Add('Content-Type', 'application/x-www-form-urlencoded');

           if gHttpclient.Post('login.microsoftonline.com/' + AadTenantId + '/oauth2/v2.0/token', gHttpcontent, ghttpResponseMessage) then begin

               ghttpResponseMessage.Content.ReadAs(gresponseText);

               gJSONToken.ReadFrom(gresponseText);

               gJSONObject := gJSONToken.AsObject();

               AccessToken := GetJSONToken(gJSONObject, 'access_token').AsValue().AsText();

           end else

               Error(FORMAT(ghttpResponseMessage.Content));

  • RE: Dynamics 365 BC -SOAP service OAuth Authentication

    Hi,

    Thanks.

    I have checked the code in the video and I am able to get records from BC successfully, but I have a issue in the post method.

    Do we have demo where we can check the Post method code of OAuth authentication using SOAP web service.

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Dynamics 365 BC -SOAP service OAuth Authentication

    Hi,

    Please go through the below link, I think it will solve your problem. there is an easy steps been demonstrated.

    navwithnav.blogspot.com/.../oauth20-token-authentication-with-al.html

  • RE: Dynamics 365 BC -SOAP service OAuth Authentication

    Hello Nitin,

    Thanks for the reply.

    I am using the below code to integrate to Purchase Invoice using SOAP web service and auth authentication. AuthTocket  tocken return successfully from my another function and I am passing Purchase Invoice object and Tocken to the below code.

    var myContent = JsonConvert.SerializeObject(purchaseInvoice);

                   var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);

                   var byteContent = new System.Net.Http.ByteArrayContent(buffer);

                   byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

                   using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())

                   {

                       client.DefaultRequestHeaders.Add("Authorization", "Bearer " + AuthToken);

                       var result = client.PostAsync(dynamicsWebServiceURL, byteContent).Result;

                       if (result.IsSuccessStatusCode)

                       {

                           Console.WriteLine(result.Content.ReadAsStringAsync().Result);

                       }

                   }

    But, for the highlighted line, I am getting error as below.

    {StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
    {
    Transfer-Encoding: chunked
    Access-Control-Allow-Headers: Origin, X-Requested-With, Authorization
    Access-Control-Allow-Origin: *
    Access-Control-Allow-Credentials: true
    ms-correlation-x: e46003d6-1968-4e51-88e2-92e01abde14b
    x-content-type-options: nosniff
    Strict-Transport-Security: max-age=31536000; includeSubDomains
    Access-Control-Expose-Headers: ms-correlation-x
    Date: Mon, 09 May 2022 11:25:18 GMT
    Server: Microsoft-HTTPAPI/2.0
    }}

    Could you please help to resolve this error?

  • Suggested answer
    Nitin Verma Profile Picture
    Nitin Verma 21,091 Super User 2024 Season 1 on at
    RE: Dynamics 365 BC -SOAP service OAuth Authentication

    Hi Narayan,

    Please use below link to learn how to generate and use OAuth.

    navwithnav.blogspot.com/.../oauth20-token-authentication-with-al.html

    Thanks.

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans