Ejemplo n.º 1
0
 private void Click_Authorize(object sender, MouseButtonEventArgs e)
 {
     try
     {
         vk = vk.Authorize("wall,photos,groups,offline,messages");
         if (vk.isAuthorized)
         {
             this.AuthStatus.Text = "Статус: Авторизация пройдена";
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show(exc.ToString());
     }
 }
Ejemplo n.º 2
0
        public Vk Authorize(string scope)
        {
            var vk     = new Vk();
            var thread = new Thread(() =>
            {
                var window = new Window {
                    Width = 800, Height = 600
                };

                Thickness brwsMargin = new Thickness(0, 0, 0, 0);
                var browser          = new WebBrowser()
                {
                    Margin = brwsMargin
                };
                window.Content = browser;

                var authLink = String.Format("https://oauth.vk.com/authorize?client_id={0}&display=popup&redirect_uri=https://oauth.vk.com/blank.html&scope={1}&response_type=token&v=5.37", appID, scope);
                browser.Navigate(authLink);

                browser.Navigated += (sender, e) =>
                {
                    if (e.Uri.GetLeftPart(UriPartial.Query) == "https://oauth.vk.com/blank.html")
                    {
                        var url          = new Uri(e.Uri.AbsoluteUri.Replace('#', '?'));
                        var parameters   = HttpUtility.ParseQueryString(url.Query);
                        var accessToken  = parameters.Get("access_token");
                        var expiresIn    = parameters.Get("expires_in");
                        var userId       = parameters.Get("user_id");
                        var isAuthorized = true;
                        vk = new Vk(accessToken, expiresIn, userId, isAuthorized);
                        window.Close();
                    }
                    else if (e.Uri.GetLeftPart(UriPartial.Query) == "https://oauth.vk.com/")
                    {
                        throw new Exception("Ошибка: Проверьте подключен ли Интеренет и пройдена ли авторизация.");
                    }
                };
                window.ShowDialog();
            });

            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            return(vk);
        }
Ejemplo n.º 3
0
        public Vk Authorize(string scope)
        {
            var vk = new Vk();
            var thread = new Thread(() =>
            {
                var window = new Window { Width = 800, Height = 600 };
                
                Thickness brwsMargin = new Thickness(0, 0, 0, 0);
                var browser = new WebBrowser() { Margin = brwsMargin };
                window.Content = browser;

                var authLink = String.Format("https://oauth.vk.com/authorize?client_id={0}&display=popup&redirect_uri=https://oauth.vk.com/blank.html&scope={1}&response_type=token&v=5.37", appID, scope);
                browser.Navigate(authLink);

                browser.Navigated += (sender, e) =>
                {
                    if (e.Uri.GetLeftPart(UriPartial.Query) == "https://oauth.vk.com/blank.html")
                    {
                        var url = new Uri(e.Uri.AbsoluteUri.Replace('#', '?'));
                        var parameters = HttpUtility.ParseQueryString(url.Query);
                        var accessToken = parameters.Get("access_token");
                        var expiresIn = parameters.Get("expires_in");
                        var userId = parameters.Get("user_id");
                        var isAuthorized = true;
                        vk = new Vk(accessToken, expiresIn, userId, isAuthorized);
                        window.Close();
                    }
                    else if (e.Uri.GetLeftPart(UriPartial.Query) == "https://oauth.vk.com/")
                    {
                        throw new Exception("Ошибка: Проверьте подключен ли Интеренет и пройдена ли авторизация.");
                    }
                };
                window.ShowDialog();
            });
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            return vk;
        }