Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Authentication with token generation for third party application using raw JSON body credentials.

(0) ShareShare
ReportReport
Posted on by 4
X++ or c# code for X++ or c# code for Authentication with token generation for third party application using raw Jason body credentials. I am getting 400 bad request error response.
 
Advance thanks for reply,
  • Verified answer
    Mohamed Amine Mahmoudi Profile Picture
    17,598 Super User 2025 Season 1 on at
    Authentication with token generation for third party application using raw Jason body credentials.
    Hi @Lakshmi,
     
    It depends on the architecture of the third-party application.
    I think your issue is fixed, then please verify the answers that helped 
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • Lakshmi Profile Picture
    13 on at
    Authentication with token generation for third party application using raw Jason body credentials.
    Thank you so much Muhamed Amine, Its a great job.I got the token!.  Additionally, I want to know how to get response from client using the same credentials in GET method.
  • Verified answer
    Mohamed Amine Mahmoudi Profile Picture
    17,598 Super User 2025 Season 1 on at
    Authentication with token generation for third party application using raw Jason body credentials.
    Hi,
     
    I have some remarks :
    1. in this case you must use application/x-www-form-urlencoded in content type
    2. the connection parameters are case sensitivity (clientId, clientSecret)
    3. the keys and values are encoded in key-value tuples separated by '&', with a '=' between the key and the value
     
    try this code :
           System.Net.HttpWebRequest request; 
    
           System.IO.Stream stream; 
    
           System.Exception sysEx; 
    
            
    
           request = System.Net.WebRequest::Create("https://inuat-gateway.c2fo.com/api/integration/v1/token") as System.Net.HttpWebRequest; 
    
           request.Method = 'POST'; 
    
           request.ContentType = 'application/x-www-form-urlencoded'; 
    
           request.Accept = '*/*'; 
    
      
    
           // Set the request headers 
    
           System.Net.WebHeaderCollection headerCollection = request.Headers; 
    
           var utf8 = System.Text.Encoding::get_UTF8(); 
    
           var byteArrayPayload = utf8.GetBytes("clientId=3haKJrCEFZLULqsfN1tq1F&clientSecret=6s1l4Xlu3Gq3z4e1w3PRE2pyS7ot8zBH3S70BjgQ9wzt3hQ/dXpF2zpAPahd5tjj"); 
    
           try 
    
           { 
    
               // send out the payload 
    
               using (System.IO.Stream dataStream = request.GetRequestStream()) 
    
               { 
    
                   dataStream.Write(byteArrayPayload, 0, byteArrayPayload.Length); 
    
               } 
    
      
    
               // request.GetResponse() may already result in an error if the request was e.g. a Bad Request(Status Code 400). This should be handled upstream via our global error handling. 
    
               using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse) 
    
               { 
    
                   stream = response.GetResponseStream(); 
    
                   System.IO.StreamReader reader = new System.IO.StreamReader(stream); 
    
                   str responseBody = reader.ReadToEnd(); 
    
               } 
    
           } 
    
           catch (sysEx) 
    
           { 
    
               throw; 
    
           } 
    Important : make sure you change the security coordinates
    Best regards,
    Mohamed Amine MAHMOUDI
  • Community member Profile Picture
    4 on at
    Authentication with token generation for third party application using raw Jason body credentials.
     

    Hi Muhamed, thanks for the reply,

    I have tried this code but I am getting bad request error, please give me a idea for get the token using json client credentials.


            System.Net.HttpWebRequest request;
            System.IO.Stream stream;
            System.Exception sysEx;
            
            request = System.Net.WebRequest::Create("https://inuat-gateway.c2fo.com/api/integration/v1/token") as System.Net.HttpWebRequest;
            request.Method = 'POST';
            request.ContentType = 'application/json';

     

     

     

            // Set the request headers
            System.Net.WebHeaderCollection headerCollection = request.Headers;
            headerCollection.Set('Name', 'Value');

     

     

     

            var utf8 = System.Text.Encoding::get_UTF8();

     

     

     

     

     

            // Set the request body

     

     

     

            var byteArrayPayload = utf8.GetBytes("{\"ClientId\":\"3haKJrCEFZLULqsfN1tq1F\",\"Clientsecret\":\"6s1l4Xlu3Gq3z4e1w3PRE2pyS7ot8zBH3S70BjgQ9wzt3hQ\"}");
           // try
           // var byteArrayPayload = utf8.GetBytes("{\"ClientId\":\"3haKJrCEFZLULqsfN1tq1F","Clientsecret\":\"6s1l4Xlu3Gq3z4e1w3PRE2pyS7ot8zBH3S70BjgQ9wzt3hQ/dXpF2zpAPahd5tjj\"}");
            try
            {
                // send out the payload
                using (System.IO.Stream dataStream = request.GetRequestStream())
                {
                    dataStream.Write(byteArrayPayload, 0, byteArrayPayload.Length);
                }

                // request.GetResponse() may already result in an error if the request was e.g. a Bad Request(Status Code 400). This should be handled upstream via our global error handling.
                using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
                {
                    stream = response.GetResponseStream();
                    System.IO.StreamReader reader = new System.IO.StreamReader(stream);
                    str responseBody = reader.ReadToEnd();
                }
            }
            catch (sysEx)
            {
                throw;
            }

  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    17,598 Super User 2025 Season 1 on at
    Authentication with token generation for third party application using raw Jason body credentials.
    Hi,
     
    can you please share with us more details.
     
    Best regards,
    Mohamed Amine MAHMOUDI

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,245 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,925 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Product updates

Dynamics 365 release plans