使用asp.net c#通过JSON获取Shopify订单数据

本文关键字:单数据 数据 Shopify 获取 net asp 通过 JSON 使用 | 更新日期: 2023-09-27 18:28:00

我正在努力构建一个非常简单的web应用程序,从Shopify的API获取订单数据并以漂亮的格式显示。

我现在是一名前端开发人员,以前我写过很多经典的asp,但asp.net和JSON对我来说都是新的

在过去的一个周末里,我在网上搜索任何可以给我快速教程的文章,或者一些关于如何完成以下操作的非常简单的示例代码:

  • 拨打Shopify API(http://docs.shopify.com/api/order#show)检索订单记录
  • 转换JSON响应以获取客户的名字和姓氏,并在他们的订单上列出行项目。然后,很好地格式化它,并将其显示在html中

我找到了json.net,并在asp.net中读到了一些关于http客户端的内容

有没有人有任何真正简单的示例代码或教程链接,像我这样的初学者可以用来学习如何使用asp.net c#来提取shopify数据并显示它?

谢谢!

使用asp.net c#通过JSON获取Shopify订单数据

我最近与shopify进行了集成。我发现最好的方法是使用RestSharp。我相信围绕着Shopify API可能有一些开源项目,但我发现他们在调用/响应方面做了很多有问题的事情。

我围绕RestSharp执行方法创建了一个基本的执行包装器

public T Execute<T>(RestRequest request) where T : new()
{
    var client = new RestClient(GetHost());
    if (AuthToken != null)
        client.Authenticator = new ShopifyAuthenticator(AuthToken);
    var result = client.Execute<T>(request);
    if(result.StatusCode == System.Net.HttpStatusCode.Unauthorized)
        throw new ShopifyUnauthorizedException(result.StatusDescription);
    if (result.ErrorException != null)
    {
        const string message = "Error retrieving response.  Check inner details for more information.";
        throw new ShopifyException(message, result.ErrorException);
    }
    return result.Data;
}
public string GetHost()
{
    return Uri.UriSchemeHttps + Uri.SchemeDelimiter + Store + _shopifyHost;
}

AuthToken是您的存储的AuthToken。Store是shopify的子域。你可以去掉异常传播的东西,直到你有了更好的理解。

还创建了一个基本的RestSharp OAuth2Authenticator。

class ShopifyAuthenticator : OAuth2Authenticator
{
    public ShopifyAuthenticator(string accessToken)
        : base(accessToken)
    {
    }
    public override void Authenticate(IRestClient client, IRestRequest request)
    {
        // only add the Authorization parameter if it hasn't been added.
        if (!request.Parameters.Any(p => p.Name.Equals("X-Shopify-Access-Token", StringComparison.OrdinalIgnoreCase)))
        {
            request.AddParameter("X-Shopify-Access-Token", AccessToken, ParameterType.HttpHeader);
        }
    }
}

那么您所要调用的就是包装器方法。

RestRequest request = new RestRequest(_shopEndpoint);
return _client.Execute<ShopResult>(request, parameters).Shop;

类和常量:

const string _shopEndpoint = "/admin/shop.json";
const string _shopifyHost = ".myshopify.com";
class ShopResult
{
    public Shop Shop { get; set; }
}
/// <summary>
/// The Shopify API's shop object is a collection of the general settings and information about the shop.
/// </summary>
public class Shop
{
    /// <summary>
    /// The shop's street address.
    /// </summary>
    public string Address1 { get; set; }
    /// <summary>
    /// The city in which the shop is located.
    /// </summary>
    public string City { get; set; }
    /// <summary>
    /// The shop's country (by default equal to the two-letter country code).
    /// </summary>
    public string Country { get; set; }
    /// <summary>
    /// The two-letter country code corresponding to the shop's country.
    /// </summary>
    public string CountryCode { get; set; }
    /// <summary>
    /// The shop's normalized country name.
    /// </summary>
    public string CountryName { get; set; }
    /// <summary>
    /// The date and time when the shop was created.
    /// </summary>
    public DateTime CreatedAt { get; set; }
    /// <summary>
    /// The customer's email.
    /// </summary>
    public string CustomerEmail { get; set; }
    /// <summary>
    /// The three-letter code for the currency that the shop accepts.
    /// </summary>
    public string Currency { get; set; }
    /// <summary>
    /// The shop's domain.
    /// </summary>
    public string Domain { get; set; }
    /// <summary>
    /// The contact email address for the shop.
    /// </summary>
    public string Email { get; set; }
    /// <summary>
    /// Feature is present when a shop has a google app domain. It will be returned as a URL. If
    /// the shop does not have this feature enabled it will default to "null."
    /// </summary>
    public string GoogleAppsDomain { get; set; }
    /// <summary>
    /// Feature is present if a shop has google apps enabled. Those shops with this feature
    /// will be able to login to the google apps login. Shops without this feature enabled will default to "null."
    /// </summary>
    public string GoogleAppsLoginEnabled { get; set; }
    /// <summary>
    /// A unique numeric identifier for the shop.
    /// </summary>
    public int Id { get; set; }
    /// <summary>
    /// Geographic coordinate specifying the north/south location of a shop.
    /// </summary>
    public string Latitude { get; set; }
    /// <summary>
    /// Geographic coordinate specifying the east/west location of a shop.
    /// </summary>
    public string Logitude { get; set; }
    /// <summary>
    /// A string representing the way currency is formatted when the currency isn't specified.
    /// </summary>
    public string MoneyFormat { get; set; }
    /// <summary>
    /// A string representing the way currency is formatted when the currency is specified.
    /// </summary>
    public string MoneyWithCurrencyFormat { get; set; }
    /// <summary>
    /// The shop's 'myshopify.com' domain.
    /// </summary>
    public string MyshopifyDomain { get; set; }
    /// <summary>
    /// The name of the shop.
    /// </summary>
    public string Name { get; set; }
    /// <summary>
    /// The name of the Shopify plan the shop is on.
    /// </summary>
    public string PlanName { get; set; }
    /// <summary>
    /// The display name of the Shopify plan the shop is on.
    /// </summary>
    public string DisplayPlanName { get; set; }
    /// <summary>
    /// Indicates whether the Storefront password protection is enabled.
    /// </summary>
    public string PasswordEnabled { get; set; }
    /// <summary>
    /// The contact phone number for the shop.
    /// </summary>
    public string Phone { get; set; }
    /// <summary>
    /// The shop's normalized province or state name.
    /// </summary>
    public string Province { get; set; }
    /// <summary>
    /// The two-letter code for the shop's province or state.
    /// </summary>
    public string ProvinceCode { get; set; }
    /// <summary>
    /// The username of the shop owner.
    /// </summary>
    public string ShopOwner { get; set; }
    /// <summary>
    /// The setting for whether applicable taxes are included in product prices: Valid values are: "true" or "null."
    /// </summary>
    public string TaxShipping { get; set; }
    /// <summary>
    /// The setting for whether applicable taxes are included in product prices. Valid values are: "true" or "null."
    /// </summary>
    public string TaxesIncluded { get; set; }
    /// <summary>
    /// The setting for whether the shop is applying taxes on a per-county basis or not (US-only). Valid values are: "true" or "null."
    /// </summary>
    public string CountyTaxes { get; set; }
    /// <summary>
    /// The name of the timezone the shop is in.
    /// </summary>
    public string Timezone { get; set; }
    /// <summary>
    /// The zip or postal code of the shop's address.
    /// </summary>
    public string Zip { get; set; }
}