Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there,

currently I'm working on an app, adding and updateting a contact via google contacts api.

It works so far, but I'm having an issue at the update procedure..

My workflow is this:

Get the specific contact, edit/add some properties, and update it to google..

But when I try to add a new email address to the contact, I get a "The remote server returned an error: (400) Bad Request." Error back. Inner Exception is null.

C#
ContactsRequest cr = new ContactsRequest(new RequestSettings(CStr.Caption, new GDataCredentials(Username, Password)));
Contact newEntry = new Contact();

//retrieve original contact
// First, retrieve the contact to update.
Contact contact = cr.Retrieve<Contact>(new Uri(GoogleTargetId));
        
//check each field like phone, mail, address, etc.
foreach(CollisionField field in list)
{
               switch(field.collisionField)
               {
                        case CollisionFieldEnum.Mail:
                        //Should this field be synced?
                        if (field.Sync)
                        {
                            try
                            {
                                //may be there is already an existing email address?

                                EMail e = contact.Emails.FirstOrDefault(
                                    x => x.Value == GoogleOrig.Mail);
                                if (e != null)
                                    e.Value = SourceContact.Mail;
                                else
                                {
                                    //no email found, so add a new one
                                    e = new EMail()
                                    {
                                        Primary = true,
                                        Value = SourceContact.Mail,
                                        Rel = ContactsRelationships.IsHome
                                    };
                                    //add this email to the collection
                                    contact.Emails.Add(e);
                                }
                                field.Synced = true;
                            }
                            catch(Exception ex)
                            {
                                CMisc.AttachToLog("Cannot update Google Contact field: " + field.collisionField.ToString() + "; " + ex.ToString());
                                field.Synced = false;
                            }
                        }
                        break;
                    //some more fields like phone, mobile, address, etc...
                    default:
                        break;


so far no problems...

I get the exception right here, when I want so submit the changes:

C#
Contact updatedC = cr.Update(contact);


I only have this problem when adding a new email address to the contact. Phone, Address, etc works all perfectly.. but not the mail.

Now I don't have any clues how to resolve this problem.
May be someone could help me please with this issue.
If you need some more informations, just let me know and I'll post it.

Thanks in advance..
Posted
Updated 27-Nov-13 11:39am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900