Adding New Contacts using v3.0 in .NET

662 views
Skip to first unread message

Azmat Ali Akhtar

unread,
Oct 17, 2011, 2:02:22 AM10/17/11
to google-co...@googlegroups.com
Using same code as in Documentation of API v3.0 to create New Contact in Google Contacts returns 400 bad request Error code is below
Please help

public static Contact CreateContact(ContactsRequest cr)
{
 
Contact newEntry = new Contact();

  newEntry
.Name = new Name()
     
{
       
FullName = "Elizabeth Bennet",
       
GivenName = "Elizabeth",
       
FamilyName = "Bennet",
     
};
  newEntry
.Content = "Notes";

  newEntry
.Emails.Add(new EMail()
     
{
       
Primary = true,
       
Rel = ContactsRelationships.IsHome,
       
Value = "l...@gmail.com"
     
});
  newEntry
.Emails.Add(new EMail()
     
{
       
Rel = ContactsRelationships.IsWork,
       
Value = "l...@example.com"
     
});

  newEntry
.Phonenumbers.Add(new PhoneNumber()
     
{
       
Primary = true,
       
Rel = ContactsRelationships.IsWork,
       
Value = "(206)555-1212",
     
});
  newEntry
.Phonenumbers.Add(new PhoneNumber()
     
{
       
Rel = ContactsRelationships.IsHome,
       
Value = "(206)555-1213",
     
});

  newEntry
.IMs.Add(new IMAddress()
     
{
       
Primary = true,
       
Rel = ContactsRelationships.IsHome,
       
Protocol = ContactsProtocols.IsGoogleTalk,
     
});

  newEntry
.PostalAddresses.Add(new StructuredPostalAddress()
     
{
       
Rel = ContactsRelationships.IsWork,
       
Primary = true,
       
Street = "1600 Amphitheatre Pkwy",
       
City ="Mountain View",
       
Region = "CA",
       
Postcode = "94043",
       
Country = "United States",
       
FormattedAddress = "1600 Amphitheatre Pkwy Mountain View",
     
});

 
Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default"));
 
Contact createdEntry = cr.Insert(feedUri, newEntry);
 
Console.WriteLine("Contact's ID: " + createdEntry.Id)
 
return createdEntry;
}

Azmat Ali Akhtar

unread,
Oct 18, 2011, 4:11:33 AM10/18/11
to google-co...@googlegroups.com
Hi
Alian please help me resolve this Problem, iam using exact same code snippet as in you API documentation along with that values and get error 400 bad request at cr.insert() methode.
Please

HK1

unread,
Oct 19, 2011, 10:42:07 AM10/19/11
to Google Contacts, Shared Contacts and User Profiles APIs
Are you sure that your ContactsRequest object (the one that gets
passed in as the argument named cr) is working? I think that this is
probably where you're problem is.

Here's VB.Net code that works for me. You should be able to convert it
to C# using one of those online code converters.

Dim conserv As New ContactsService("MyCompany-MyApp-v1.0")
Dim authFactory As New GDataGAuthRequestFactory("cp",
"MyCompany-MyApp-v1.0")
authFactory.AccountType = "GOOGLE"
conserv.RequestFactory = authFactory
conserv.setUserCredentials("yourem...@gmail.com",
"YourPassword")
conserv.QueryClientLoginToken()
Dim nc As New ContactEntry
With nc
.Name = New Google.GData.Extensions.Name()
.Name.FullName = "John Davis"
.Name.GivenName = "John"
.Name.FamilyName = "Davis"
End With
Dim org As New Google.GData.Extensions.Organization()
With org
.Rel =
Google.GData.Extensions.ContactsRelationships.IsWork
.Name = "Garfield"
.Primary = True
End With
nc.Organizations.Add(org)
Dim ph As New
Google.GData.Extensions.PhoneNumber("111-222-3333")
With ph
.Primary = True
.Rel =
Google.GData.Extensions.ContactsRelationships.IsWork
'I'm not sure when you need to use this
'.Uri = "tel:+1111222333"
End With
nc.Phonenumbers.Add(ph)
Dim urifeed As New
Uri(ContactsQuery.CreateContactsUri("default"))
conserv.Insert(urifeed, nc)

Alain Vongsouvanh

unread,
Oct 20, 2011, 11:45:43 AM10/20/11
to google-co...@googlegroups.com
Hello Azmat,

There is indeed a "bug" in the docs which should be fixed shortly. The correct way to add an e-mail address is like that:


  newEntry.Emails.Add(new EMail()
     
{
       
Primary = true,
       
Rel = ContactsRelationships.IsHome,

       
Address = "l...@gmail.com" // <-- Change here.
     
});

Best,
Alain

--
You received this message because you are subscribed to the Google
Groups "Google Contacts, Shared Contacts and User Profiles APIs" group.
To post to this group, send email to
google-co...@googlegroups.com
To unsubscribe from this group, send email to
google-contacts...@googlegroups.com
For more options, visit this group at
http://code.google.com/apis/contacts/community/forum.html



--
Alain Vongsouvanh | Developer Programs Engineer
Reply all
Reply to author
Forward
0 new messages