Exemplo n.º 1
0
        private bool getAccessToken(string authCode)
        {
            bool   getSuccess  = false;
            string accessToken = "";

            if (RefreshKey_saved == "")
            {
                consumer.ClientCredentialApplicator =
                    ClientCredentialApplicator.PostParameter(clientSecret);

                IAuthorizationState grantedAccess1 = consumer.ProcessUserAuthorization(authCode);

                accessToken     = grantedAccess1.AccessToken;
                RefreshKey_real = grantedAccess1.RefreshToken;

                // save key
                iniParser parser = new iniParser();

                parser.IniParser(iniPath);
                parser.AddSetting("Setup", "refreshkey", grantedAccess1.AccessToken);
                parser.AddSetting("Setup", "refreshkey_real", grantedAccess1.RefreshToken);
                parser.SaveSettings();
                myTabControl.SelectedIndex = 0;
            }
            else
            {
                accessToken = RefreshKey_saved;
                myTabControl.SelectedIndex = 0;
            }
            try
            {
                GoogleApi api = new GoogleApi(accessToken);

                // string user = "******"; // api.GetEmail();
                // GoogleApi api = new GoogleApi(accessToken);

                XmlDocument         contacts = api.GetContacts();
                XmlNamespaceManager nsmgr    = new XmlNamespaceManager(contacts.NameTable);
                //

                XmlNodeList _title = contacts.GetElementsByTagName("title");
                string      temp   = _title.Item(0).InnerText;
                temp        = temp.Replace("'s Contacts", "");
                sender_name = temp;
                if (textbox_name.Text == "")
                {
                    textbox_name.Text = temp;
                }

                nsmgr.AddNamespace("gd", "http://schemas.google.com/g/2005");
                nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom");
                emailCount = 0;

                foreach (XmlNode contact in contacts.GetElementsByTagName("entry"))
                {
                    XmlNode title = contact.SelectSingleNode("a:title", nsmgr);
                    XmlNode email = contact.SelectSingleNode("gd:email", nsmgr);

                    // Console.WriteLine("{0}: {1}",title.InnerText, email.Attributes["address"].Value);
                    if (email != null)
                    {
                        title_arr.Add(title.InnerText);
                        gmail_arr.Add(email.Attributes["address"].Value);
                        emailCount++;
                    }
                }
                getSuccess = true;
                button_import.Visibility   = Visibility.Collapsed;
                image_gmail.Visibility     = System.Windows.Visibility.Visible;
                connected_gmail.Visibility = image_gmail.Visibility;
                myTabControl.SelectedIndex = 0;
            }
            catch (Exception err)
            {
                Console.WriteLine("Error: " + err.Message);
                getSuccess = false;
                return(getSuccess);
            }
            // everything is good, goto input : autocomplete
            contactData.inc(emailCount);
            int i = 0;

            foreach (string emailAddress in gmail_arr)
            {
                contactData.States.SetValue(emailAddress, i);
                i++;
            }
            int where = contactData.States.Length;
            //
            AutoCompleteBox.ItemsSource = contactData.States;
            //email_list.Items.Clear();
            label_invalid.Visibility = Visibility.Collapsed;
            // myTabControl.SelectedIndex = 0;
            return(getSuccess);
        }
Exemplo n.º 2
0
        private bool getAccessToken(string authCode)
        {
            bool   getSuccess  = false;
            string accessToken = "";

            if (RefreshKey_saved == "")
            {
                consumer.ClientCredentialApplicator =
                    ClientCredentialApplicator.PostParameter(clientSecret);

                IAuthorizationState grantedAccess1 = consumer.ProcessUserAuthorization(authCode);
                bool result = consumer.RefreshAuthorization(grantedAccess1, TimeSpan.FromHours(10));

                accessToken = grantedAccess1.AccessToken;

                // save key
                iniParser parser       = new iniParser();
                String    appStartPath = System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
                parser.IniParser(iniPath);
                string _r = grantedAccess1.AccessToken; //.RefreshToken;
                parser.AddSetting("Setup", "refreshkey", _r);
                parser.SaveSettings();
                myTabControl.SelectedIndex = 0;
            }
            else
            {
                // change code immediately
                //  consumer.RefreshAuthorization(grantedAccess1, TimeSpan.FromDays(30));
                //accessToken = grantedAccess1.AccessToken;
                accessToken = RefreshKey_saved;
                myTabControl.SelectedIndex = 0;
            }
            try
            {
                GoogleApi api = new GoogleApi(accessToken);


                // string user = "******"; // api.GetEmail();
                // GoogleApi api = new GoogleApi(accessToken);

                XmlDocument contacts = api.GetContacts();

                XmlNamespaceManager nsmgr = new XmlNamespaceManager(contacts.NameTable);
                nsmgr.AddNamespace("gd", "http://schemas.google.com/g/2005");
                nsmgr.AddNamespace("a", "http://www.w3.org/2005/Atom");
                emailCount = 0;
                foreach (XmlNode contact in contacts.GetElementsByTagName("entry"))
                {
                    XmlNode title = contact.SelectSingleNode("a:title", nsmgr);
                    XmlNode email = contact.SelectSingleNode("gd:email", nsmgr);

                    Console.WriteLine("{0}: {1}",
                                      title.InnerText,
                                      email.Attributes["address"].Value);
                    mail_arr.Add(email.Attributes["address"].Value);
                    emailCount++;
                    //   listbox1.Items.Add(title.InnerText + " , " + email.Attributes["address"].Value);
                }
                getSuccess = true;
            }
            catch (Exception err)
            {
                // MessageBox.Show("error: " + err.Message);
                Console.WriteLine("Error: " + err.Message);
                getSuccess = false;
                return(getSuccess);
            }
            // everything is good, goto input : autocomplete
            contactData.inc(emailCount);
            int i = 0;

            foreach (string emailAddress in mail_arr)
            {
                contactData.States.SetValue(emailAddress, i);
                i++;
            }
            int where = contactData.States.Length;
            //
            AutoCompleteBox.ItemsSource = contactData.States;
            email_list.Items.Clear();
            label_invalid.Visibility = Visibility.Collapsed;
            // myTabControl.SelectedIndex = 0;
            return(getSuccess);

//------------------------
            try
            {
                // get inbox mail content
                // #region get InBox
                string user = "******"; // api.GetEmail();
                using (Imap imap = new Imap())
                {
                    imap.ConnectSSL("imap.gmail.com");
                    imap.LoginOAUTH2(user, accessToken);

                    imap.SelectInbox();
                    List <long> uids = imap.Search(Flag.Unseen);

                    foreach (long uid in uids)
                    {
                        string eml   = imap.GetMessageByUID(uid);
                        IMail  email = new MailBuilder().CreateFromEml(eml);

                        // listbox1.Items.Add(email.From);
                    }
                    imap.Close();
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("error: " + err.Message);
            }
        }