Looking for a working example of C# HttpWebRequest for createOrReplaceInventoryItemInventory
Hi,
I have had great success using the REST API to download order details, greatly reducing the workflow between getting an Order and processing the details into my accounting platform. I use a couple of recursive functions to capture any OAuth Token refreshment but the crux of the API interaction is achieved with the following code snippet; again this is just an example of how my Order Download function works...
try
{
string apiEndpoint = "https://api.ebay.com/sell/fulfillment/v1/order/" + orderNumber;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiEndpoint);
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add("Authorization", "Bearer " + _oauthUserToken);
request.ContentType = "application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
string responseString = reader.ReadToEnd();
// Deserialize the response string to EbayOrder object.
...
}
BUT, when I look for code examples to apply what I assume is a correctly formatted PUT request...
string apiEndpoint = "https://api.ebay.com/sell/inventory/v1/inventory_item/" + sku;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(apiEndpoint);
request.Method = "PUT";
request.Accept = "application/json";
request.Headers.Add("Authorization", "Bearer " + _oauthUserToken);
request.Headers.Add("Content-Language", "en-US");
request.ContentType = "application/json";... I just get a 500 Error. ChatGPT has tried to give me some examples but none are giving me the expected 204 response. Such as...
// Create the HttpClient
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {_oauthUserToken}");
// Construct the API endpoint URL
string url = "https://api.ebay.com/sell/inventory/v1/inventory_item/" + sku;
// Send the PUT request to add the inventory item
HttpResponseMessage response = client.PutAsync(url, null).GetAwaiter().GetResult();
But this doesn't work either.
Once the above is answered I still need to work out the remaining 3 steps of:
- Create your inventory item using createOrReplaceInventoryItem call
- Create an inventory location for the created item through the createInventoryLocation API call. Your offer won't publish without this.
- Create an offer for the inventory item. Specify the offer details, such as fulfillment, payment, return policy IDs, category ID, inventory location ID, etc
- Publish the offer.
... As hard as all this is, it will still beat the hell out of trying to list 100 new items using eBays latest awful Web Portal for creating new listing...
Fingers crossed there's a benevolent developer out there !!
cottontech_pty_ltd
OP3 years agoAnswered my own question : ) ChatGPT and I finally worked out you can't apply a null value in client.PutAsync. Modified code snippet reads:
Hope this helps someone. The _inventoryItem is a simple class built initally from the demo item you can create in the API Explorer, an Apple watch or something... The Class is defined below but the actual properties will change depending on your inventory item...