Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics AX forum
Unanswered

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

Posted on by Microsoft Employee

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.

Categories:
  • Community Member Profile Picture
    Community Member Microsoft Employee 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
    Martin Dráb 225,588 Super User 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.

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,986 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,588 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans