Ejemplo n.º 1
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;
            }
        }
Ejemplo n.º 2
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;
            }
        }