Skip to main content

Notifications

Announcements

No record found.

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.

  • Community Member Profile Picture
    on at
    RE: ebay api via System.Net.HttpWebRequest (POST body information)

    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;
    }

  • Martin Dráb Profile Picture
    231,925 Most Valuable Professional on at
    RE: ebay api via System.Net.HttpWebRequest (POST body information)

    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.

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