Exemplo n.º 1
0
        private void buttonRequestTokenAndPin_Click(object sender, EventArgs e)
        {
            try {
                string url;
                string key, secret;
                if (this.comboBoxTarget.SelectedIndex == 1)
                {
                    key    = textBoxConsumerKey.Text;
                    secret = textBoxConsumerSecret.Text;
                    url    = "https://api.etrade.com/";
                }
                else
                {
                    key    = textBoxSandboxKey.Text;
                    secret = textBoxSandBoxSecret.Text;
                    url    = "https://apisb.etrade.com/";
                }

                Manager manager = new Manager();
                manager["consumer_key"]    = key;
                manager["consumer_secret"] = secret;
                OAuthResponse requestToken =
                    manager.AcquireRequestToken(url + "oauth/request_token", "POST");


                textBoxResponse.Text = "Request Token succeeded" + Environment.NewLine;
                // +requestToken.AllText; // <- shows the response

                string responseToken  = requestToken["oauth_token"];
                string responseSecret = requestToken["oauth_token_secret"];

                string authorizeURL = "https://us.etrade.com/e/t/etws/authorize?key=" +
                                      manager["consumer_key"] + "&token=" + requestToken["oauth_token"];

                System.Diagnostics.Process.Start(authorizeURL);

                FormGetVerifyKey form = new FormGetVerifyKey();
                if (form.ShowDialog() == DialogResult.OK)
                {
                    string pin = form.Pin;
                    manager = new Manager();
                    manager["consumer_key"]    = key;
                    manager["consumer_secret"] = secret;
                    manager["token"]           = responseToken;
                    manager["token_secret"]    = responseSecret;

                    // Now get access token
                    string URL_ACCESS_TOKEN = url + "oauth/access_token";
                    string sentHeader       = "";
                    try {
                        var responseAuth = manager.AcquireAccessToken(URL_ACCESS_TOKEN,
                                                                      "POST", pin, out sentHeader);

                        textBoxResponse.Text = responseAuth.AllText;
                    } catch (Exception ex) {
                        textBoxResponse.Text += Environment.NewLine + "Get Access Token Failed";
                        textBoxResponse.Text += Environment.NewLine + "Sent: " + sentHeader + Environment.NewLine;
                        textBoxResponse.Text += Environment.NewLine + "Exception" + Environment.NewLine + ex.Message;
                        return;
                    }
                }
            } catch (Exception ex) {
                textBoxResponse.Text = "Exception" + Environment.NewLine + ex.Message;
                return;
            }
        }
Exemplo n.º 2
0
        public async Task <bool?> Init(MetroWindow win)
        {
            try
            {
                parentWindow = win as MainWindow;

                authMgr = new Manager();
                authMgr["consumer_key"]    = ConsumerKey;
                authMgr["consumer_secret"] = ConsumerSecret;
                authMgr.AcquireRequestToken(RequestTokenURL, "POST");
                MetroDialogSettings settings = new MetroDialogSettings()
                {
                    AnimateHide           = true,
                    AnimateShow           = true,
                    AffirmativeButtonText = "확인",
                    NegativeButtonText    = "종료"
                };

                MessageDialogResult result = await DialogManager.ShowMessageAsync(parentWindow, "인증", "인증을 위해 웹브라우저를 통해 트위터에 로그인합니다.",
                                                                                  MessageDialogStyle.AffirmativeAndNegative, settings);

                if (result == MessageDialogResult.Affirmative)
                {
                    System.Diagnostics.Process.Start($"{RequestPinURL}{authMgr["token"]}");
                    settings = new MetroDialogSettings()
                    {
                        AnimateHide           = true,
                        AnimateShow           = true,
                        AffirmativeButtonText = "인증",
                        NegativeButtonText    = "취소 및 종료"
                    };

                    string pin = await DialogManager.ShowInputAsync(parentWindow, "PIN", "웹브라우저 상에 표시된 PIN을 입력합니다.", settings);

                    try
                    {
                        authMgr.AcquireAccessToken(AccessTokenURL, "POST", pin);
                    }
                    catch (Exception ex)
                    {
                        parentWindow.Close();
                    }

                    IsInit = true;

                    // Get Screen Name
                    dynamic obj = await GetJson("https://api.twitter.com/1.1/account/settings.json", "GET");

                    string name = obj.screen_name;

                    // GetProfile
                    dynamic json = await GetJson($"https://api.twitter.com/1.1/users/show.json?screen_name={name}", "GET");

                    parentWindow.UserNameBlock.Text    = json.name;
                    parentWindow.ScreenNameBlock.Text  = $"@{json.screen_name}";
                    parentWindow.ProfileDescBlock.Text = json.description;
                    string imageUrl = json.profile_image_url;
                    ProfileImage = new BitmapImage();
                    ProfileImage.BeginInit();
                    ProfileImage.UriSource          = new Uri(imageUrl.Replace("_normal", ""));
                    ProfileImage.DownloadCompleted +=
                        (s, ev) => OnPropertyChanged("ProfileImage");
                    ProfileImage.EndInit();
                }
                else
                {
                    parentWindow.Close();
                }

                return(IsInit);
            }
            catch (Exception e)
            {
                await DialogManager.ShowMessageAsync(parentWindow, "Error", $"= Message{e.Message}\n\n= Stack Trace\n{e.StackTrace}");

                IsInit = false;
                return(false);
            }
        }