添加Google通讯录无效

时间:2013-06-13 16:50:48

标签: c# google-contacts

我得到了这个代码“工作”(读取不抛出异常)。但是联系人没有添加到我的gmail联系人中(也不是我的Android手机上同步联系人)。

请注意,我可以正确阅读联系人,因此凭据是正确的。

我读到我应该检查请求中的状态,但我看到的唯一状态是ContactEntry的属性,它始终为null。

这是一个用于测试的控制台应用程序。

public static void AddContact(ContactDetail contact)
        {
            GContactService = new ContactsService("Contact Infomation");
            GContactService.setUserCredentials("myemail@gmail.com", "mypassword");

            ContactEntry newEntry = new ContactEntry();
            newEntry.Title.Text = contact.Name;
            newEntry.Name = new Name() { FullName = "Tristan Savage", GivenName = "Tristan", FamilyName = "Savage"};

            EMail primaryEmail = new EMail(contact.EmailAddress1);
            primaryEmail.Primary = true;
            primaryEmail.Rel = ContactsRelationships.IsWork;
            newEntry.Emails.Add(primaryEmail);

            PhoneNumber phoneNumber = new PhoneNumber(contact.Phone);
            phoneNumber.Primary = true;
            phoneNumber.Rel = ContactsRelationships.IsMobile;
            newEntry.Phonenumbers.Add(phoneNumber);

            PostalAddress postalAddress = new PostalAddress();
            postalAddress.Value = contact.Address;
            postalAddress.Primary = true;
            postalAddress.Rel = ContactsRelationships.IsCompanyMain;
            newEntry.PostalAddresses.Add(new StructuredPostalAddress() { City = "montreal", Label = "Bureau"});

            newEntry.Content.Content = contact.Details;

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

            ContactEntry createdEntry = (ContactEntry)GContactService.Insert(feedUri, newEntry); 

        }

2 个答案:

答案 0 :(得分:2)

我终于明白了。需要GroupMembership才能让联系人联系到移动设备!

这是缺少的部分:

var groupMembership = new GroupMembership
{
    HRef = "http://www.google.com/m8/feeds/groups/" + utilisateur.CourrielGmailContacts + "/base/6"
};
newEntry.GroupMembership.Add(groupMembership);

答案 1 :(得分:1)

尝试评论一些行。只有姓名和电子邮件,您应该能够创建联系人。

我可以在这里创建一个.NET示例联系人: https://developers.google.com/google-apps/contacts/v3/?hl=fr#creating_contacts

以这种方式初始化Request: ContactsRequest Request = new ContactsRequest(new RequestSettings("appName", "user@gmail.com", "password"));

请注意,您的新联系人未与群组相关联。因此,您不会在“我的联系人”/“Mes Contacts”中看到该联系人。您应该在“其他联系人”/“Autres contacts”中看到它。

相关问题