/// <summary> /// A convenience function that tries to ensure that a given URL is a valid Shopify store by checking the response headers for X-ShopId. /// </summary> /// <param name="url">The URL of the shop to check.</param> /// <returns>A boolean indicating whether the URL is valid.</returns> public static async Task <bool> IsValidMyShopifyUrl(string url) { Uri uri = RequestEngine.BuildShopUri(url); RestClient client = RequestEngine.CreateClient(uri); //Make request RestRequest request = new RestRequest("", Method.GET); IRestResponse response = await client.ExecuteTaskAsync(request); //Valid Shopify stores will have an X-ShopId header return(response.Headers.Any(h => h.Name == "X-ShopId")); }
/// <summary> /// Authorizes an application installation, generating an access token for the given shop. /// </summary> /// <param name="code">The authorization code generated by Shopify, which should be a parameter named 'code' on the request querystring.</param> /// <param name="myShopifyUrl">The store's *.myshopify.com URL, which should be a paramter named 'shop' on the request querystring.</param> /// <param name="shopifyApiKey">Your app's public API key.</param> /// <param name="shopifySecretKey">Your app's secret key.</param> /// <returns>The shop access token.</returns> public static async Task <string> Authorize(string code, string myShopifyUrl, string shopifyApiKey, string shopifySecretKey) { Uri shopUri = RequestEngine.BuildShopUri(myShopifyUrl); RestClient client = RequestEngine.CreateClient(shopUri); IRestRequest req = RequestEngine.CreateRequest("oauth/access_token", Method.POST); JToken response; //Build request body req.AddJsonBody(new { client_id = shopifyApiKey, client_secret = shopifySecretKey, code }); response = await RequestEngine.ExecuteRequestAsync(client, req); return(response.Value <string>("access_token")); }
/// <summary> /// Creates a new instance of <see cref="ShopifyService" />. /// </summary> /// <param name="myShopifyUrl">The shop's *.myshopify.com URL.</param> /// <param name="shopAccessToken">An API access token for the shop.</param> protected ShopifyService(string myShopifyUrl, string shopAccessToken) { _ShopUri = RequestEngine.BuildShopUri(myShopifyUrl); _RestClient = RequestEngine.CreateClient(_ShopUri, shopAccessToken); }