Esempio n. 1
0
        public HttpWebRequest CreateAuthorizedRequest(string url, string requestMethod, ApiFilter filter, string act, string atsc)
        {
            AccessToken       = act;
            AccessTokenSecret = atsc;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + filter.ToString());

            OAuthBase oAuth     = new OAuthBase();
            string    nonce     = oAuth.GenerateNonce();
            string    timeStamp = oAuth.GenerateTimeStamp();
            string    parameters;
            string    normalizedUrl;
            string    signature = oAuth.GenerateSignature(new Uri(url), ConsumerKey, ConsumerSecret,
                                                          AccessToken, AccessTokenSecret, requestMethod, timeStamp, nonce, OAuthBase.SignatureTypes.PLAINTEXT,
                                                          out normalizedUrl, out parameters);

            StringBuilder sb = new StringBuilder("OAuth ");

            sb.AppendFormat("oauth_token=\"{0}\",", AccessToken);
            sb.AppendFormat("oauth_version=\"{0}\",", "1.0");
            sb.AppendFormat("oauth_signature_method=\"{0}\",", "PLAINTEXT");
            sb.AppendFormat("oauth_nonce=\"{0}\",", nonce);
            sb.AppendFormat("oauth_timestamp=\"{0}\",", timeStamp);
            sb.AppendFormat("oauth_consumer_key=\"{0}\",", ConsumerKey);
            sb.AppendFormat("oauth_signature=\"{0}\"", signature);

            request.Headers[HttpRequestHeader.Authorization] = sb.ToString();
            request.Method = requestMethod;

            //request.ContentType = "application/json";
            request.Accept    = "text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8";//application/json,
            request.KeepAlive = true;

            return(request);
        }
Esempio n. 2
0
        private void BeginAuthorization()
        {
            var uri = new Uri("http://smeitproducts.com/magento-beta/oauth/initiate");


            OAuthBase oAuth     = new OAuthBase();
            string    nonce     = oAuth.GenerateNonce();
            string    timeStamp = oAuth.GenerateTimeStamp();
            string    parameters;
            string    normalizedUrl;


            string signature = oAuth.GenerateSignature(uri, ConsumerKey, ConsumerSecret,
                                                       String.Empty, String.Empty, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.PLAINTEXT,
                                                       out normalizedUrl, out parameters);


            StringBuilder requestUri = new StringBuilder(uri.ToString());

            requestUri.AppendFormat("?oauth_consumer_key={0}&", ConsumerKey);
            requestUri.AppendFormat("oauth_callback={0}&", "http://localhost:4893/Default.aspx");
            requestUri.AppendFormat("oauth_nonce={0}&", nonce);
            requestUri.AppendFormat("oauth_timestamp={0}&", timeStamp);
            requestUri.AppendFormat("oauth_signature_method={0}&", "PLAINTEXT");
            requestUri.AppendFormat("oauth_version={0}&", "1.0");
            requestUri.AppendFormat("oauth_signature={0}", signature);


            var imgclient = new RestClient(requestUri.ToString());

            var imgrequest = new RestSharp.RestRequest(RestSharp.Method.POST)
            {
                RequestFormat = RestSharp.DataFormat.Xml
            };

            var imgresponse = imgclient.Execute(imgrequest);
            var dd          = imgresponse.Content;

            string[] res = dd.Split('&');
            string   tok = res[0];

            string[] authToken = tok.Split('=');

            string tok2 = res[1];

            string[] authToken2 = tok2.Split('=');

            Session["oauth_token_secret"] = authToken2[1];


            string redirectUrl = "http://smeitproducts.com/magento-beta/index.php/admin/oauth_authorize?oauth_token=" + authToken[1];

            Response.Redirect(redirectUrl);
        }
Esempio n. 3
0
        protected void Page_Load(object sender, EventArgs e)
        {
            string ConsumerKey    = "a47a1f116d74f7c0608b2ac904dec6d1";
            string ConsumerSecret = "4ba049c97fd17cf0fbb275ee39f59fc9";

            string oauth_token        = WebConfigurationManager.AppSettings["Magento.Token"];
            string oauth_token_secret = WebConfigurationManager.AppSettings["Magento.TokenSecret"];

            var uri = new Uri("http://smeitproducts.com/magento-beta" + "/api/rest/products");

            OAuthBase oAuth     = new OAuthBase();
            string    nonce     = oAuth.GenerateNonce();
            string    timeStamp = oAuth.GenerateTimeStamp();
            string    parameters;
            string    normalizedUrl;
            string    signature = oAuth.GenerateSignature(uri, ConsumerKey, ConsumerSecret,
                                                          oauth_token, oauth_token_secret, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.PLAINTEXT,
                                                          out normalizedUrl, out parameters);

            StringBuilder requestUri = new StringBuilder("OAuth ");

            requestUri.AppendFormat("oauth_token=\"{0}\",", oauth_token);
            requestUri.AppendFormat("oauth_version=\"{0}\",", "1.0");
            requestUri.AppendFormat("oauth_signature_method=\"{0}\",", "PLAINTEXT");
            requestUri.AppendFormat("oauth_nonce=\"{0}\",", nonce);
            requestUri.AppendFormat("oauth_timestamp=\"{0}\",", timeStamp);
            requestUri.AppendFormat("oauth_consumer_key=\"{0}\",", ConsumerKey);
            requestUri.AppendFormat("oauth_signature=\"{0}\"", signature);

            string     BASE_URL   = "http://smeitproducts.com/magento-beta";
            RestClient restClient = new RestClient(BASE_URL);

            RestRequest restRequest = new RestRequest("/api/rest/products", Method.GET);

            restRequest.AddHeader("Authorization", requestUri.ToString());
            restRequest.RequestFormat = DataFormat.Json;


            var response = restClient.Execute(restRequest);
            var data     = response.Content;
        }
Esempio n. 4
0
        private void Authorize(string oauth_token, string oauth_verifier)
        {
            var    uri = new Uri(MagentoServer + "/oauth/token");
            string oauth_token_secret = (string)Session["oauth_token_secret"];

            OAuthBase oAuth     = new OAuthBase();
            string    nonce     = oAuth.GenerateNonce();
            string    timeStamp = oAuth.GenerateTimeStamp();
            string    parameters;
            string    normalizedUrl;
            string    signature = oAuth.GenerateSignature(uri, ConsumerKey, ConsumerSecret,
                                                          oauth_token, oauth_token_secret, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.PLAINTEXT,
                                                          out normalizedUrl, out parameters);

            StringBuilder sb = new StringBuilder("OAuth ");

            sb.AppendFormat("oauth_verifier=\"{0}\",", oauth_verifier);
            sb.AppendFormat("oauth_token=\"{0}\",", oauth_token);
            sb.AppendFormat("oauth_version=\"{0}\",", "1.0");
            sb.AppendFormat("oauth_signature_method=\"{0}\",", "PLAINTEXT");
            sb.AppendFormat("oauth_nonce=\"{0}\",", nonce);
            sb.AppendFormat("oauth_timestamp=\"{0}\",", timeStamp);
            sb.AppendFormat("oauth_consumer_key=\"{0}\",", ConsumerKey);
            sb.AppendFormat("oauth_signature=\"{0}\"", signature);



            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.Headers[HttpRequestHeader.Authorization] = sb.ToString();
            request.ContentType = "text/xml";
            request.Accept      = "text/xml";
            request.KeepAlive   = true;
            request.Method      = "POST";

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream       responseStream = response.GetResponseStream();
                    StreamReader responseReader = new StreamReader(responseStream);
                    string       text           = responseReader.ReadToEnd();
                    try
                    {
                        Dictionary <String, string> responseDic = GetDictionaryFromQueryString(text);

                        string token  = responseDic.First(q => q.Key == "oauth_token").Value;
                        string secret = responseDic.First(q => q.Key == "oauth_token_secret").Value;

                        Configuration      objConfig      = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
                        AppSettingsSection objAppsettings = (AppSettingsSection)objConfig.GetSection("appSettings");
                        //Edit
                        if (objAppsettings != null)
                        {
                            objAppsettings.Settings["Magento.Token"].Value       = token;
                            objAppsettings.Settings["Magento.TokenSecret"].Value = secret;
                            objConfig.Save();


                            var Resturi = new Uri("http://smeitproducts.com/magento-beta" + "/api/rest/products");

                            OAuthBase oAuth2        = new OAuthBase();
                            string    Restnonce     = oAuth2.GenerateNonce();
                            string    ResttimeStamp = oAuth2.GenerateTimeStamp();
                            string    Restparameters;
                            string    RestnormalizedUrl;
                            string    signature2 = oAuth2.GenerateSignature(Resturi, ConsumerKey, ConsumerSecret,
                                                                            token, secret, "GET", ResttimeStamp, Restnonce, OAuthBase.SignatureTypes.PLAINTEXT,
                                                                            out RestnormalizedUrl, out Restparameters);

                            StringBuilder requestUri = new StringBuilder("OAuth ");
                            requestUri.AppendFormat("oauth_token=\"{0}\",", token);
                            requestUri.AppendFormat("oauth_version=\"{0}\",", "1.0");
                            requestUri.AppendFormat("oauth_signature_method=\"{0}\",", "PLAINTEXT");
                            requestUri.AppendFormat("oauth_nonce=\"{0}\",", Restnonce);
                            requestUri.AppendFormat("oauth_timestamp=\"{0}\",", ResttimeStamp);
                            requestUri.AppendFormat("oauth_consumer_key=\"{0}\",", ConsumerKey);
                            requestUri.AppendFormat("oauth_signature=\"{0}\"", signature2);

                            string     BASE_URL   = "http://smeitproducts.com/magento-beta";
                            RestClient restClient = new RestClient(BASE_URL);

                            RestRequest restRequest = new RestRequest("/api/rest/products", Method.GET);
                            restRequest.AddHeader("Authorization", requestUri.ToString());
                            restRequest.RequestFormat = DataFormat.Json;


                            var response2 = restClient.Execute(restRequest);
                            var data      = response2.Content;
                        }

                        errorLabel.Text      = "Done";
                        errorLabel.ForeColor = System.Drawing.Color.Green;
                    }
                    catch (Exception ex)
                    {
                        errorLabel.Text = "Exchanging token failed.<br>Response text = " + text + "<br>Exception = " + ex.Message;
                    }
                }
            }
            catch (WebException ex)
            {
                var          responseStream = ex.Response.GetResponseStream();
                StreamReader responseReader = new StreamReader(responseStream);
                string       resp           = responseReader.ReadToEnd();
                errorLabel.Text = resp;
            }
        }
Esempio n. 5
0
        private void BeginAuthorization()
        {
            string CallbackUrl = Server.UrlEncode(Request.Url.AbsoluteUri);
            var    uri         = new Uri(MagentoServer + "/oauth/initiate?oauth_callback=" + CallbackUrl);

            OAuthBase oAuth     = new OAuthBase();
            string    nonce     = oAuth.GenerateNonce();
            string    timeStamp = oAuth.GenerateTimeStamp();
            string    parameters;
            string    normalizedUrl;
            string    signature = oAuth.GenerateSignature(uri, ConsumerKey, ConsumerSecret,
                                                          String.Empty, String.Empty, "GET", timeStamp, nonce, OAuthBase.SignatureTypes.PLAINTEXT,
                                                          out normalizedUrl, out parameters);

            StringBuilder sb = new StringBuilder("OAuth ");

            sb.AppendFormat("oauth_callback=\"{0}\",", CallbackUrl);
            sb.AppendFormat("oauth_version=\"{0}\",", "1.0");
            sb.AppendFormat("oauth_signature_method=\"{0}\",", "PLAINTEXT");
            sb.AppendFormat("oauth_nonce=\"{0}\",", nonce);
            sb.AppendFormat("oauth_timestamp=\"{0}\",", timeStamp);
            sb.AppendFormat("oauth_consumer_key=\"{0}\",", ConsumerKey);
            sb.AppendFormat("oauth_signature=\"{0}\"", signature);

            var request = (HttpWebRequest)WebRequest.Create(uri);

            request.Headers[HttpRequestHeader.Authorization] = sb.ToString();
            request.ContentType = "text/xml";
            request.Accept      = "text/xml";
            request.KeepAlive   = true;
            request.Method      = "GET";

            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream       responseStream = response.GetResponseStream();
                    StreamReader responseReader = new StreamReader(responseStream);
                    string       text           = responseReader.ReadToEnd();
                    try
                    {
                        Dictionary <String, string> dic = GetDictionaryFromQueryString(text);

                        string oauth_token        = dic.First(q => q.Key == "oauth_token").Value;
                        string oauth_token_secret = dic.First(q => q.Key == "oauth_token_secret").Value;
                        Session["oauth_token_secret"] = oauth_token_secret;
                        string redirectUrl = MagentoServer + "/index.php/admin/oauth_authorize?oauth_token=" + oauth_token + "&oauth_verifier=" +
                                             oauth_token_secret;
                        Response.Redirect(redirectUrl);
                    }
                    catch (Exception ex)
                    {
                        errorLabel.Text = "Parsing request token failed.<br>Response text = " + text + "<br>Exception = " + ex.Message;
                    }
                }
            }
            catch (WebException ex)
            {
                var          responseStream = ex.Response.GetResponseStream();
                StreamReader responseReader = new StreamReader(responseStream);
                string       resp           = responseReader.ReadToEnd();
                errorLabel.Text = resp;
            }
        }