我正在尝试构建一个非常简单的Web应用程序,该应用程序从Shopify的API中获取订单数据并以漂亮的格式显示它。
白天我是前端开发人员,在我写了很多经典asp的那天,但asp.net和JSON对我来说都是新手。
我上周末在网上搜索任何可以给我一个快速教程的文章,或者一些非常简单的示例代码,用于如何执行以下操作:
我找到了json.net,我读了一下asp.net中的httpclient。
有没有人有任何非常简单的示例代码或任何教程链接,像我这样的初学者可以用来学习如何使用asp.net c#来提取shopify数据并显示它?
谢谢!
答案 0 :(得分: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; }
}