/// <summary>
        /// Builds an authorization URL for Shopify OAuth integration.
        /// </summary>
        /// <param name="scopes">An array of Shopify permission strings, e.g. 'read_orders' or 'write_script_tags'. These are the permissions that your app needs to run.</param>
        /// <param name="myShopifyUrl">The shop's *.myshopify.com URL.</param>
        /// <param name="shopifyApiKey">Your app's public API key.</param>
        /// <param name="redirectUrl">An optional URL to redirect the user to after integration. Overrides the Shopify app's default redirect URL.</param>
        /// <param name="state">An optional, random string value provided by your application which is unique for each authorization request. During the OAuth callback phase, your application should check that this value matches the one you provided to this method.</param>
        /// <param name="grants">Requested grant types, which will change the type of access token granted upon OAuth completion. Only known grant type is "per-user", which will give an access token restricted to the permissions of the user accepting OAuth integration and will expire when that user logs out. Leave the grants array empty or null to receive a full access token that doesn't expire.</param>
        /// <returns>The authorization url.</returns>
        public static Uri BuildAuthorizationUrl(IEnumerable <string> scopes, string myShopifyUrl, string shopifyApiKey, string redirectUrl = null, string state = null, IEnumerable <string> grants = null)
        {
            //Prepare a uri builder for the shop URL
            var builder = new UriBuilder(RequestEngine.BuildShopUri(myShopifyUrl));

            //Build the querystring
            var qs = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("client_id", shopifyApiKey),
                new KeyValuePair <string, string>("scope", string.Join(",", scopes)),
            };

            if (string.IsNullOrEmpty(redirectUrl) == false)
            {
                qs.Add(new KeyValuePair <string, string>("redirect_uri", redirectUrl));
            }

            if (string.IsNullOrEmpty(state) == false)
            {
                qs.Add(new KeyValuePair <string, string>("state", state));
            }

            if (grants != null && grants.Count() > 0)
            {
                foreach (var grant in grants)
                {
                    qs.Add(new KeyValuePair <string, string>("grant_options[]", grant));
                }
            }

            builder.Path  = "admin/oauth/authorize";
            builder.Query = string.Join("&", qs.Select(s => $"{s.Key}={s.Value}"));

            return(builder.Uri);
        }
Example #2
0
        /// <summary>
        /// Builds an authorization URL for Shopify OAuth integration.
        /// </summary>
        /// <param name="scopes">An array of <see cref="ShopifyAuthorizationScope"/> — the permissions that your app needs to run.</param>
        /// <param name="myShopifyUrl">The shop's *.myshopify.com URL.</param>
        /// <param name="shopifyApiKey">Your app's public API key.</param>
        /// <param name="redirectUrl">An optional URL to redirect the user to after integration. Overrides the Shopify app's default redirect URL.</param>
        /// <param name="state">An optional, random string value provided by your application which is unique for each authorization request. During the OAuth callback phase, your application should check that this value matches the one you provided to this method.</param>
        /// <returns>The authorization url.</returns>
        public static Uri BuildAuthorizationUrl(IEnumerable <ShopifyAuthorizationScope> scopes, string myShopifyUrl, string shopifyApiKey, string redirectUrl = null, string state = null)
        {
            //Prepare a uri builder for the shop URL
            UriBuilder builder = new UriBuilder(RequestEngine.BuildShopUri(myShopifyUrl));

            //Build the querystring
            List <KeyValuePair <string, string> > qs = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("client_id", shopifyApiKey),
                new KeyValuePair <string, string>("scope", string.Join(",", scopes.Select(s => s.ToSerializedString()))),
            };

            if (string.IsNullOrEmpty(redirectUrl) == false)
            {
                qs.Add(new KeyValuePair <string, string>("redirect_uri", redirectUrl));
            }

            if (string.IsNullOrEmpty(state) == false)
            {
                qs.Add(new KeyValuePair <string, string>("state", state));
            }

            builder.Path  = "admin/oauth/authorize";
            builder.Query = string.Join("&", qs.Select(s => $"{s.Key}={s.Value}"));

            return(builder.Uri);
        }
        /// <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);
 }