有人得到了。运行一个简单的帖子到shopify创建订单的网络示例我已经看到一些非常古老的例子,但他们似乎没有工作那些做回答的没有代码示例我尝试以下但只是得到错误的请求回来,当然这应该是简单的?
private static async Task runShopifyAsyncCreateOrderJsonTask()
{
using (var handler = new HttpClientHandler { Credentials = GetCredential() })
using (var client = new HttpClient(handler))
{
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string json = @"{'order': {'line_items': [{'variant_id': 720123393,'quantity': 1}]}}";
// HTTP post
HttpResponseMessage response = await client.PostAsJsonAsync("admin/orders.json", test);
if (response.IsSuccessStatusCode)
{
Uri uri = response.Headers.Location;
var results = await response.Content.ReadAsStringAsync();
}
}
}
答案 0 :(得分:1)
我知道这篇文章已经过时但如果有人遇到它,请查看这些课程: ShopifyApi.cs,ShopifyClient.cs,Program.cs。它使用的WebClient可能更容易使用,但仍然可以解决问题。
答案 1 :(得分:0)
经过一番游戏后,我发现这可行:
string ShopPath = "/admin/api/2020-04/";
string shopifyAddProduct = "products.json";
string shopifyGetProductFmt = "products.json?title={0}";
string URL = String.Format("https://{0}.myshopify.com{1}", ShopName, ShopPath);
var clientHandler = new HttpClientHandler
{
Credentials = new NetworkCredential(APIkey/*username*/, APIpass/*Password*/),
PreAuthenticate = true
};
HttpClient client = new HttpClient(clientHandler);
client.BaseAddress = new Uri(URL);
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// need an access token...
client.DefaultRequestHeaders.Add("X-Shopify-Access-Token", APIpass);
var shopify = new
{
product = new
{
title = "Burton Custom Freestyle 151",
body_html = "<strong>Good snowboard!</strong>",
vendor = "Snowboard",
product_type = "Snowboard",
published = false
}
};
// before we try and add a product, we should query if we already have it?
string shopifyGetProduct = String.Format(shopifyGetProductFmt, Uri.EscapeUriString(shopify.product.title));
HttpResponseMessage getResponse = client.GetAsync(shopifyGetProduct).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs.
if (getResponse.IsSuccessStatusCode)
{
bool foundProduct;
foundProduct = false;
// Parse the response body.
GetProducts curProduct = getResponse.Content.ReadAsAsync<GetProducts>().Result; //Make sure to add a reference to System.Net.Http.Formatting.dll
if (curProduct != null)
{
foreach (var cp in curProduct.products)
{
Console.WriteLine("found id: {0}", cp.id);
foundProduct = true;
}
}
if (!foundProduct)
{
HttpResponseMessage response = client.PostAsJsonAsync(shopifyAddProduct, shopify).Result; // Blocking call! Program will wait here until a response is received or a timeout occurs.
if (response.IsSuccessStatusCode)
{
// Parse the response body.
AddProducts newProduct = response.Content.ReadAsAsync<AddProducts>().Result; //Make sure to add a reference to System.Net.Http.Formatting.dll
if ((newProduct != null) && (newProduct.product != null))
{
Console.WriteLine("new id: {0}", newProduct.product.id);
}
}
else
{
Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
}
}
}
else
{
Console.WriteLine("{0} ({1})", (int)getResponse.StatusCode, getResponse.ReasonPhrase);
}
//Dispose once all HttpClient calls are complete. This is not necessary if the containing object will be disposed of; for example in this case the HttpClient instance will be disposed automatically when the application terminates so the following call is superfluous.
client.Dispose();
没有代码不是真正的异步-我将由您自己解决-我是在演示如何在Shopify中使用HttpClient,而不是在演示如何编写异步代码。
可以通过将返回的JSON粘贴到带有“特殊粘贴”的Visual Studio中来生成AddProduct类。但是,“ Root”类有点困难,因此我将其粘贴在这里:
public class AddProducts
{
public Product product { get; set; }
}
public class GetProducts
{
public Product[] products { get; set; }
}
祝你好运!