コード例 #1
0
        /// <summary>
        /// Authorizes an application installation, generating an access token for the given shop.
        /// </summary>
        /// <param name="clientId">App id</param>
        /// <param name="clientSecret">App secret key</param>
        /// <param name="refreshToken">refresh token.</param>
        /// <returns>The shop access token.</returns>
        public static async Task <WixAuthorizationResult> GenerateNewAccessToken(string clientId, string clientSecret, string refreshToken)
        {
            var ub = new UriBuilder(WixService.BuildWixUri("www.wix.com"))
            {
                Path = "oauth/access"
            };

            var content = new JsonContent(new
            {
                refresh_token = refreshToken,
                client_secret = clientSecret,
                client_id     = clientId,
                grant_type    = "refresh_token"
            });

            using (var client = new HttpClient())
                using (var msg = new CloneableRequestMessage(ub.Uri, HttpMethod.Post, content))
                {
                    var request       = client.SendAsync(msg);
                    var response      = await request;
                    var rawDataString = await response.Content.ReadAsStringAsync();

                    WixService.CheckResponseExceptions(response, rawDataString);
                    return(JsonConvert.DeserializeObject <WixAuthorizationResult>(rawDataString));
                }
        }
コード例 #2
0
        /// <summary>
        /// Authorizes an application installation, generating an access token for the given shop.
        /// </summary>
        /// <param name="code">The authorization code generated by Wix, which should be a parameter named 'code' on the request querystring.</param>
        /// <param name="wixAppKey">Your app's public API key.</param>
        /// <param name="wixAppSecretKey">Your app's secret key.</param>
        /// <returns>The authorization result.</returns>
        public static async Task <WixAuthorizationResult> AuthorizeWithResult(string code, string wixAppKey, string wixAppSecretKey)
        {
            var ub = new UriBuilder(WixService.BuildWixUri("www.wix.com"))
            {
                Path = "oauth/access"
            };
            var content = new JsonContent(new
            {
                code,
                client_secret = wixAppSecretKey,
                client_id     = wixAppKey,
                grant_type    = "authorization_code"
            });

            using (var client = new HttpClient())
                using (var msg = new CloneableRequestMessage(ub.Uri, HttpMethod.Post, content))
                {
                    var request       = client.SendAsync(msg);
                    var response      = await request;
                    var rawDataString = await response.Content.ReadAsStringAsync();

                    WixService.CheckResponseExceptions(response, rawDataString);
                    return(JsonConvert.DeserializeObject <WixAuthorizationResult>(rawDataString));
                }
        }
コード例 #3
0
        /// <summary>
        /// Builds an authorization URL for Wix OAuth integration.
        /// </summary>
        /// <param name="wixAppId">Your wix app Id</param>
        /// <param name="redirectUrl">URL to redirect the user to after integration.</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 BuildAuthorizationUrlUsingToken(string wixAppId, string redirectUrl, string state = null)
        {
            //Prepare a uri builder for the shop URL
            var builder = new UriBuilder(WixService.BuildWixUri("www.wix.com"));

            //Build the querystring
            var qs = new List <KeyValuePair <string, string> >
            {
                new KeyValuePair <string, string>("appId", wixAppId),
                new KeyValuePair <string, string>("redirectUrl", redirectUrl)
            };

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

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

            return(builder.Uri);
        }