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

Generation Oauth Token from C# library and consuming it in D365 FO (x++)

(0) ShareShare
ReportReport
Posted on by 184

Hi All,

I have requirement to integrate D365 FO with and 3rd party tool.

I have Payment API provided by client with.

I have client ID,secret, scope and token URL

I am trying to generate token and consume it in the API call.

I have written C# library for the same and then I have added it as reference in x project.

I have created a runnable class and trying to call the C# code but I am getting the below error.

Please correct if I am not following right procedure to achieve this.

C# Code

using System;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;

namespace WTL_6DeeAPIToken
{
    public class SixDeeTokenService
    {
        public static  async Task GET6DeeToken()
        {
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("https://xxxxxxxxxxxxx.onmicrosoft.com/gis/.default");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            string tokenURL = @"https://login.microsoftonline.com/24f9d756-bf0c-43e9-ad5e-2073ae2d6698/oauth2/v2.0/token";

            string grant_type = "client_credentials";
            string client_id = "xxxxxxxxxxxxxxxxxxxx";
            string client_secret = "xxxxxxxxxxxxxxxxxxxxx";

            var form = new Dictionary
            {
                {"grant_type", grant_type},
                {"client_id", client_id},
                {"client_secret", client_secret},
            };

            HttpResponseMessage tokenResponse = await client.PostAsync(tokenURL, new FormUrlEncodedContent(form));
            var jsonContent = await tokenResponse.Content.ReadAsStringAsync();
            Token tok = JsonConvert.DeserializeObject(jsonContent);
            return tok;

        }

        public class Token
        {
            [JsonProperty("access_token")]
            public string AccessToken { get; set; }

            [JsonProperty("token_type")]
            public string TokenType { get; set; }

            [JsonProperty("expires_in")]
            public int ExpiresIn { get; set; }

            [JsonProperty("refresh_token")]
            public string RefreshToken { get; set; }
        }
    }
}

X Code:

6dAPIError.PNG

I have the same question (0)
  • Komi Siabi Profile Picture
    13,049 Most Valuable Professional on at
    RE: Generation Oauth Token from C# library and consuming it in D365 FO (x++)

    Hi,

    I believe this is the continuation of this:

    community.dynamics.com/.../code-to-call-apis-from-x

    By the way, can you tell us with screenshot how you added the reference  to your X++ project?

  • D365_Wibes Profile Picture
    184 on at
    RE: Generation Oauth Token from C# library and consuming it in D365 FO (x++)

    I have copied the dll the from the C# library project and pasted it in "K:\AosService\PackagesAndLocalDirectories\<MyModel>\<MyModel>\bin

    then in my x++ project I have added it as reference

    Please let me know what am I doing wrong here?

  • D365_Wibes Profile Picture
    184 on at
    RE: Generation Oauth Token from C# library and consuming it in D365 FO (x++)

    6Dref.PNG

  • Suggested answer
    Komi Siabi Profile Picture
    13,049 Most Valuable Professional on at
    RE: Generation Oauth Token from C# library and consuming it in D365 FO (x++)

    You should remove the reference, rebuild your C# Project then re-add the reference and build X++.

  • D365_Wibes Profile Picture
    184 on at
    RE: Generation Oauth Token from C# library and consuming it in D365 FO (x++)

    {
      "order_information": {
        "payment": {
          "ext_profile_id": "3ace35c3-6443-eb11-a812-000d3a20f116",
          "transaction_id": "12323553434",
          "amount": "25.00",
          "payment_detail": [
            {
              "payment_mode": "1",
              "amount_paid": "25",
              "reference_external_id": "4520941365",
              "card_holder_name": "Patrisia Allen",
              "mode_detail": [
                {
                  "value": "113cvxrdu1",
                  "key": "sample_key"
                }
              ]
            }
          ],
          "account_id": "1000107754",
          "payment_gw_token": "vsE0aZWzPq02376",
          "comment": "order payment",
          "auth_token": "xrzy6ryymust",
          "currency_code": "QAR"
        },
        "customer_name": "Harryson Jerico",
        "order_type": "MakePayment",
        "dataset": {
          "param": [
            {
              "id": "",
              "value": ""
            }
          ]
        },
        "contact_number": "(555) 555-1234"
      }
    }

    Hi Siabi,

    Can you please suggest me how can I generate the below JSON payload in x ?

  • Verified answer
    Martin Dráb Profile Picture
    236,503 Most Valuable Professional on at
    RE: Generation Oauth Token from C# library and consuming it in D365 FO (x++)

    It's just a string, therefore you could easily create this string by concentation of the components. But the usual approach is using data contract classes. The top level is would be a contract class with a single member: order_information. This would refer to another contract class with a single property, payment. It'll use another class, this time with members like ext_profile_id and amount. And so on.

    You'll out data to the contract objects and serialize the root object to JSON.

  • Suggested answer
    Komi Siabi Profile Picture
    13,049 Most Valuable Professional on at
    RE: Generation Oauth Token from C# library and consuming it in D365 FO (x++)

    Hi D365_Wibes;

    [quote user="D365_Wibes"]Can you please suggest me how can I generate the below JSON payload in x++ ?[/quote]

    Please, can you confirm if the error you posted originally had been addressed. If yes, kindly close this thread ask a new question and experts would provide possible approach to achieve this.

  • D365_Wibes Profile Picture
    184 on at
    RE: Generation Oauth Token from C# library and consuming it in D365 FO (x++)

    thanks martin , I have used JSON writer to develop this .

  • Suggested answer
    Komi Siabi Profile Picture
    13,049 Most Valuable Professional on at
    RE: Generation Oauth Token from C# library and consuming it in D365 FO (x++)

    Hi D365_Wibes,

    Great, you are able to generate JSON payload in your X++ project.

    What about the error your runnable class, is it also resolved now?

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

#1
CA Neeraj Kumar Profile Picture

CA Neeraj Kumar 1,882

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 525 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans