使用ApiV3与Google Update联系人出错

时间:2015-11-09 10:30:06

标签: c# google-contacts

我正在使用Google Contacts API v3.0,我可以使用c#成功登录gmail。我也获得了访问代码,并且我能够成功检索联系人。之后,我想更新联系人,但我无法更新与以下代码的联系。

Contact newEntry = new Contact();
            cr.GetGroups("My Contacts");
            if (dr.Table.Columns.Contains("FirstName") || dr.Table.Columns.Contains("LastName"))
            {
                newEntry.Name = new Name()
                {
                    FullName = Convert.ToString(dr["FirstName"]) + " " +   Convert.ToString(dr["LastName"]),
                    GivenName = Convert.ToString(dr["FirstName"]),
                    FamilyName = Convert.ToString(dr["LastName"])
                };
            }
            newEntry.Content = "Notes";
            if (dr.Table.Columns.Contains("Email"))
            {
                if (dr["Email"] != DBNull.Value)
                    newEntry.Emails.Add(new EMail()
                    {
                        Primary = true,
                        Rel = ContactsRelationships.IsHome,
                        Address = Convert.ToString(dr["Email"])
                    });
                //newEntry.Emails.Add(new EMail()
                //{
                //    Rel = ContactsRelationships.IsWork,
                //    Address = "g@example.com"
                //});
            }
            if (dr.Table.Columns.Contains("PhoneHome"))
            {
                newEntry.Phonenumbers.Add(new PhoneNumber()
                {
                    Primary = true,
                    Rel = ContactsRelationships.IsHome,
                    Value = Convert.ToString(dr["PhoneHome"]),
                });
            }

            if (dr.Table.Columns.Contains("PhoneWork"))
            {
                newEntry.Phonenumbers.Add(new PhoneNumber()
                {
                    Rel = ContactsRelationships.IsWork,
                    Value = Convert.ToString(dr["PhoneWork"]),
                });
            }
            // Set the contact's phone numbers.

            // Insert the contact.
            Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default"));

            Feed<Group> fg = cr.GetGroups();
            GroupMembership gm = new GroupMembership();
            foreach (Group group in fg.Entries)
            {
                gm.HRef = group.Id;
                if (group.Title.ToLower() == "System Group: My Contacts".ToLower())
                    break;
            }

            newEntry.GroupMembership.Add(gm);
            try
            {
            Contact contact = cr.Retrieve<Contact>(feedUri);
            if (contact == null)
                contact = cr.Insert(feedUri, newEntry);
            else
                contact = cr.Update(newEntry);
            }
            catch (GDataRequestException ex)
            {

            }
            catch (Exception ex)
            {
            }

如何找到Google的联系人。我在Google上创建时,我的数据库中没有联系人ID。

1 个答案:

答案 0 :(得分:0)

您可以尝试并调用Updated类来检查它是否实际更新。

public static Contact UpdateContactName(ContactsRequest cr, Uri contactURL)
{
  // First, retrieve the contact to update.
  Contact contact = cr.Retrieve<Contact>(contactURL);
  contact.Name.FullName = "New Name";
  contact.Name.GivenName = "New";
  contact.Name.FamilyName = "Name";
  try
  {
    Contact updatedContact = cr.Update(contact);
    Console.WriteLine("Updated: " + updatedEntry.Updated.ToString())
    return updatedContact;
  }
  catch (GDataVersionConflictException e)
  {
    // Etags mismatch: handle the exception.
  }
  return null;
}

可以在Google Contacts API页面找到更多信息。

希望这有帮助!