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, ...
Suggested answer

Error: Object reference not set to an instance of an object when calling a web api (token url)

(0) ShareShare
ReportReport
Posted on by 1,552

Hi

so i called a token url (authorization) using postman and i got the result correctly where i filled client Id, client  secret and client credentials.

However when i did this by code I'm getting an error at this line " System.IO.Stream dataStream = request.GetRequestStream()" as you can see below

System.Text.Encoding encoding  = System.Text.Encoding::UTF8;
Password clientSecret                   = cryptoblob2str(WinAPIServer::cryptUnProtectData(clientSecret1)); //clientSecret1 is defined
System.Byte[] bytes                     = encoding.GetBytes(ClientId   ":"   clientSecret); //client id is defined
System.String base64                    = System.Convert::ToBase64String(bytes); 

System.Net.WebHeaderCollection header   = new System.Net.WebHeaderCollection();
System.String headerKey                 = "Authorization";
System.String headerValue               = "Basic "   base64;
header.Add(headerKey,headerValue);

System.Net.WebRequest request   = System.Net.WebRequest::Create(tokenURL);//token url is defined
request.Method                  = "POST";
request.set_Headers(header);
request.set_ContentType("application/x-www-form-urlencoded");

str bodyKeyValue        = "grant_type=client_credentials";
System.Byte[] byteArray = encoding.GetBytes(bodyKeyValue);
request.set_ContentLength(byteArray.get_Length());

using(System.IO.Stream dataStream = request.GetRequestStream()) //Error is here
{
    dataStream.Write(byteArray, 0, byteArray.get_Length());
}

Any idea what i'm doing wrong

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: Error: Object reference not set to an instance of an object when calling a web api (token url)

    Hi,

    is this related to D365FO? It seems like some general C# code. Anyway, one thing is common with D365FO. The first thing that you need to do is to debug it to understand better why it fails.

    But since it's not a D365FO question, perhaps you can post it to some other forum such as StackOverflow.

  • junior AX Profile Picture
    1,552 on at
    RE: Error: Object reference not set to an instance of an object when calling a web api (token url)

    Hi Nikolaos,

    i wrote that in a d365fo class... you can use system.net in addition to x++. So i wanted to call a web api from d365fo by code

  • nmaenpaa Profile Picture
    101,160 Moderator on at
    RE: Error: Object reference not set to an instance of an object when calling a web api (token url)

    Ok, thanks!

    Then you can go to my previous post to "The first thing that you need to do...".

  • junior AX Profile Picture
    1,552 on at
    RE: Error: Object reference not set to an instance of an object when calling a web api (token url)

    Hi Nikolaos,

    I'm debugging but there is nothing to indicate what's wrong it just fails when it reaches this point, even though the request is filled

  • Martin Dráb Profile Picture
    236,471 Most Valuable Professional on at
    RE: Error: Object reference not set to an instance of an object when calling a web api (token url)

    What's in StackTrace property of the exception?

  • junior AX Profile Picture
    1,552 on at
    RE: Error: Object reference not set to an instance of an object when calling a web api (token url)

    Hi Martin,

    I'm not sure i got what you mean

    but i put the code in a seperate runnable class and i got the error unable to connect to remote server

    pastedimage1602687672899v1.png

    and this is the stackTrace

    pastedimage1602687698449v2.png

  • Martin Dráb Profile Picture
    236,471 Most Valuable Professional on at
    RE: Error: Object reference not set to an instance of an object when calling a web api (token url)

    Check out details of the exception, such as Status (StackTrace isn't important in this case; that was for the other exception).

    There may be many reasons why you're unable to connect, such as a wrong address or authorization.

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,964

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 564 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans