【问题标题】:Getting Shopify order data with asp.net c# via JSON通过 JSON 使用 asp.net c# 获取 Shopify 订单数据
【发布时间】:2014-10-28 03:16:13
【问题描述】:

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

我白天是一名前端开发人员,过去我写了很多经典的 asp,但是 asp.net 和 JSON 对我来说都是新的。

上周末我在网上搜寻任何可以为我提供快速教程的文章,或者一些非常简单的示例代码来说明如何执行以下操作:

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

我找到了 json.net,并在 asp.net 中阅读了一些关于 httpclient 的信息。

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

谢谢!

【问题讨论】:

    标签: c# asp.net json json.net shopify


    【解决方案1】:

    我最近与 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; }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-10-26
      • 2016-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多