web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

ebay api via System.Net.HttpWebRequest (POST body information)

(0) ShareShare
ReportReport
Posted on by

Hello,

I am in the process of building an API for Ebay interfaces. I need to send some client credentials in the "body":

grant_type=client_credentials&scope=api.ebay.com/.../api_scope

I use the class System.Net.HttpWebRequest.

However, I don't know how to post something in the body. Can anyone here possibly help me how I could simply implement it?

I have attached a code snippet:

private str sExecuteRequest(str _sSubURL, str _sRESTMethod, System.Net.WebHeaderCollection _oHeader)
{
    str                           sResponse;
    System.Net.HttpWebRequest     oRequest;
    System.Net.HttpWebResponse    oResponse;
    System.Net.

//   System.Net.NetworkCredential  oCredentials;
    System.IO.StreamReader        oReader;
    System.IO.Stream              oDataStream;

    CLRObject                     oClrObj;
    ;

    oClrObj = System.Net.WebRequest::Create(_sSubURL);
    oRequest = oClrObj;

//Create Header 
    oRequest.set_Method(_sRESTMethod);

    oRequest.set_Headers(_oHeader);
    oRequest.set_ContentType(#HeaderContentType);
    oRequest.set_Accept(#HeaderContentAccept);

 //   "grant_type", "client_credentials&scope=https://api.ebay.com/oauth/api_scope"

    oResponse = oRequest.GetResponse();
    oDataStream = oResponse.GetResponseStream();
    oReader = new System.IO.StreamReader(oDataStream);
    sResponse = oReader.ReadToEnd();

    oReader.Close();
    oDataStream.Close();
    oResponse.Close();

    return sResponse;
}

I get the error message above the tool Fiddler:

{"error":"unsupported_grant_type","error_description":"grant type in request is not supported by the authorization server"}


Thank you in advance.

I have the same question (0)
  • Martin Dráb Profile Picture
    239,451 Most Valuable Professional on at

    It seems that you're trying to get an authentication token and the body should look like this (not like what you have in your comment):

    {
    	"grant_type" : "client_credentials",
    	"scope" : "https://api.ebay.com/oauth/api_scope"
    }

    But you don't seem to be setting any body at all. Use oRequest.GetRequestStream() to get a stream and write the data there, before calling GetResponse().

    By the way, you're calling Close() on resource if everything ends successfully, but you won't close them if an exception occurs.

  • Community Member Profile Picture
    on at

    Exactly. First, i need for a authorisation token, which I use for an access token.

    According to Ebay customer service:

    "I checked the logs and provided request body details and I found grant_type & scope list value is not correct.

    Please specify grant_type and scope correctly.

    Ex. grant_type=client_credentials&scope=api.ebay.com/.../api_scope

    Best Regards,

    eBay Developer Support"

    ... The grant type and scope should look like this. I have now adjusted the code and according to the tool Fiddler, an http message: 200 (OK) comes back. However, I noticed in the debugger, that the execute "oResponse = oRequest.GetResponse();" the AX hangs on.

    If I lower the "Timeout", then the connection breaks prematurely.

    [About the "exception": This method is called by another method and is in a "try/catch clause".

    I have attached the revised code:

    private str sExecuteRequest(str _sSubURL, str _sRESTMethod, System.Net.WebHeaderCollection _oHeader)
    {
        str                           sResponse;
        str                           sBody = "grant_type=client_credentials&scope=https://api.ebay.com/oauth/api_scope";
    
        System.Net.HttpWebRequest     oRequest;
        System.Net.HttpWebResponse    oResponse;
        System.Net.HttpWebResponse    oResponseM;
        System.IO.StreamReader        oReader;
        System.IO.Stream              oDataStream, ORequestStream;
        
        System.Exception              oError;
        System.Text.Encoding          utf8;
        System.Byte[]                 byteArray;
    
        CLRObject                     oClrObj;
        ;
    
    //Create Web-Object
        System.Net.ServicePointManager::set_SecurityProtocol(System.Net.SecurityProtocolType::Ssl3);
        oClrObj = System.Net.WebRequest::Create(_sSubURL);
        oRequest = oClrObj;
    
    //Create Header
        oRequest.set_Method(_sRESTMethod);
        oRequest.set_KeepAlive(false);
        oRequest.set_Headers(_oHeader);
        oRequest.set_ContentType(#HeaderContentType);
        oRequest.set_Accept(#HeaderContentAccept);
        oRequest.set_Timeout(10000);
    
        utf8 = System.Text.Encoding::get_UTF8();
        byteArray = utf8.GetBytes(sBody);
    
        ORequestStream = oRequest.GetRequestStream();
        ORequestStream.Write(byteArray, 0, byteArray.get_Length());
    
    //Execute Request and Response
        oResponse = oRequest.GetResponse();
        oDataStream = oResponse.GetResponseStream();
        oReader = new System.IO.StreamReader(oDataStream);
        sResponse = oReader.ReadToEnd();
        
        oReader.Close();
        oDataStream.Close();
        oResponse.Close();
    
        CodeAccessPermission::revertAssert();
    
        return sResponse;
    }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 620

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 521 Super User 2026 Season 1

#3
CP04-islander Profile Picture

CP04-islander 430

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans