Beispiel #1
0
 /// <summary>
 /// Log off and close CS
 /// </summary>
 private void Logoff()
 {
     if (_isLogon)
     {
         LoggerProvider.Instance.Logger.Debug("Log off from Google account");
         _cr      = null;
         _isLogon = false;
     }
 }
        public string GoogleContactLookup(string oAuthToken, string lookup)
        {
            try
            {
                if (oAuthToken.IsNullOrBlank())
                {
                    Log("Error calling GoogleContactLookup, the OAuth token parameter was empty.");
                    return null;
                }
                else if (lookup.IsNullOrBlank())
                {
                    Log("Error calling GoogleContactLookup, the lookup parameter was empty.");
                    return null;
                }
                else
                {
                    Log("Starting Google Contact Lookup for " + lookup + ".");

                    Google.GData.Client.OAuth2Parameters parameters = new Google.GData.Client.OAuth2Parameters
                    {
                        ClientId = _googleApiClientID,
                        ClientSecret = _googleApiClientSecret,
                        AccessToken = "x", //accessToken,
                        RefreshToken = oAuthToken.Trim()
                    };
                    Google.GData.Client.RequestSettings settings = new Google.GData.Client.RequestSettings("SIPSorcery-DialPlan", parameters);
                    Google.Contacts.ContactsRequest cr = new Google.Contacts.ContactsRequest(settings);
                    //Google.GData.Client.Feed<Google.Contacts.Contact> feed = cr.GetContacts();
                    Google.GData.Client.Feed<Google.Contacts.Contact> feed = cr.Get<Google.Contacts.Contact>(
                        new System.Uri("https://www.google.com/m8/feeds/contacts/default/full?q=" + lookup + "&v=3.0"));

                    if (feed != null && feed.Entries != null && feed.Entries.Count() > 0)
                    {
                        var entry = feed.Entries.First();

                        if (entry.Name != null && entry.Name.FullName.NotNullOrBlank())
                        {
                            Log("Result found Google Contact Lookup for " + lookup + " of " + entry.Name.FullName + ".");
                            return entry.Name.FullName;
                        }
                        else
                        {
                            Log("A result was found Google Contact Lookup for " + lookup + " but the FullName field was empty.");
                            return null;
                        }
                    }
                    else
                    {
                        Log("No result was found with a Google Contact Lookup for " + lookup + ".");
                        return null;
                    }
                }

                //return GoogleContactLookupAysnc(username).Result;

                //ContactsService service = new ContactsService("sipsorcery-lookup");

                //((GDataRequestFactory)service.RequestFactory).KeepAlive = false;

                //service.setUserCredentials(username, password);
                //var result = service.QueryClientLoginToken();

                //Log("Google contact authentication result " + result + ".");

                //var query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
                //query.ExtraParameters = "q=" + lookup + "&max-results=1";

                //ContactsFeed feed = service.Query(query);

                //if (feed != null && feed.Entries != null && feed.Entries.Count > 0)
                //{
                //    var entry = feed.Entries.First() as ContactEntry;

                //    if (entry.Name != null && entry.Name.FullName.NotNullOrBlank())
                //    {
                //        Log("Result found Google Contact Lookup for " + lookup + " of " + entry.Name.FullName + ".");
                //        return entry.Name.FullName;
                //    }
                //    else
                //    {
                //        Log("A result was found Google Contact Lookup for " + lookup + " but the FullName field was empty.");
                //        return null;
                //    }
                //}
                //else
                //{
                //    Log("No result was found with a Google Contact Lookup for " + lookup + ".");
                //    return null;
                //}
            }
            catch (Exception excp)
            {
                Log("Exception in GoogleContactLookup. " + excp.Message);
                return null;
            }
        }
        public void SyncGoogleContacts() {

            using (ISession session = SessionFactory.OpenSession()) {

                if (bool.Parse(GetSetting(session, "google_sync", "false"))) {

                    using (ITransaction trx = session.BeginTransaction()) {

                        try {

                            Google.Contacts.ContactsRequest cr = new Google.Contacts.ContactsRequest(
                                new RequestSettings("AsteriskCid", new GDataCredentials(
                                                                    GetSetting(session, "google_login", ""),
                                                                    GetSetting(session, "google_password", ""))));

                            Feed<Google.Contacts.Contact> f = cr.GetContacts();
                            f.AutoPaging = true;
                            foreach (Google.Contacts.Contact entry in f.Entries) {

                                if (entry.Name != null && entry.Name.FullName != null) {

                                    Contact acidContact = new Contact(entry.Name.FullName);

                                    if (entry.PhotoEtag != null) {
                                        acidContact.SetPhoto(cr.Service.Query(entry.PhotoUri));
                                    }

                                    foreach (Google.GData.Extensions.PhoneNumber number in entry.Phonenumbers) {

                                        if (session.Get<PhoneNumber>(number.Value) != null) {
                                            log.WarnFormat("Phone number {0} already exist, skipping this number", number.Value);
                                            continue;
                                        }

                                        PhoneNumber phoneNumber = new PhoneNumber(number.Value, acidContact);
                                        acidContact.PhoneNumbers.Add(phoneNumber);
                                        session.Save(phoneNumber);
                                    }

                                    if (acidContact.PhoneNumbers.Count > 0)
                                        session.Save(acidContact);
                                }
                            }

                            trx.Commit();

                        } catch (Exception e) {
                            log.Error("Failed to synchronize google contacts", e);
                            trx.Rollback();
                            throw;
                        }
                    }
                }
            }
        }