使用ApiV3更新Google联系人时出错

本文关键字:出错 联系人 Google ApiV3 更新 使用 | 更新日期: 2023-09-27 18:21:06

我正在使用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)
            {
            }

我如何从谷歌上找到联系人。当我在谷歌上创建时,我的数据库中没有联系人id。

使用ApiV3更新Google联系人时出错

您可以尝试并调用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联系人API页面上找到。

希望这能有所帮助!