OAuth and Contacts API

47 views
Skip to first unread message

andreroy55

unread,
Feb 23, 2009, 2:57:37 PM2/23/09
to Google Contacts API
I've been going over this for a few days and so far can't find
anything that addresses what I'm looking for.

I need to update about 2000 users from our current mail system to
Google Apps.

Step 1. Transfer their contacts. Many of them have grouped their
contacts, and I need to replicate that.

I want to use OAuth because that way I won't have to log in as each
individual user to update their contacts.

I'm using C# (.NET) to do this and .. well, I can't find much.

The documentation for the Contacts API for .NET skips the entire
concept of Contact Groups and it doesn't look like any of it adresses
OAuth. I've managed to get some stuff working with OAuth, like add
contacts to a user's "base" or infiled contacts, I can also add
groups.

I'd love to figure out how to list a user's contact groups and how to
add contacts to a group. I did find some pseudo code the other
day ... 1. get a list of ContactGroups with their IDs 2. add a
contact to the group. Exactly what I want to do, but I can't find how
to do it.

I don't find this: http://google-gdata.googlecode.com/svn/docs/index.html#
very useful, it seems that once I know what I want, it would be great,
but I don't quite know what I want or need.

I guess, what I really want is some documentation that shows me how to
use the API, all that I find is what it is.

Sorry to sound all complainy like that, it just gets very frustrating.
I'll keep looking ....

Julian (Google)

unread,
Feb 24, 2009, 12:38:15 PM2/24/09
to Google Contacts API
Hi,

It is true that the documentation for Contacts in .NET is not in good
shape, but, we are working on getting it up to date.

Here is a sample that should help you get users groups, create new
groups and add a group to users, I hope it helps.

Cheers,
Julian.

Sample:

ContactsService contactsService = new ContactsService("myDemoApps");

// Using ClientLogin username/password authentication
contactsService.setUserCredentials("us...@domain.com", "Password");

// Uri for Group operations for the user
Uri groupUri = new Uri(GroupsQuery.CreateGroupsUri
("us...@domain.com"));

// List all Groups for the user
GroupsFeed groupsFeed = contactsService.Query(new GroupsQuery
(groupUri.AbsoluteUri));
foreach (GroupEntry entry in groupsFeed.Entries)
{
Console.WriteLine( entry.Title.Text + "\r\n " );
Console.WriteLine( entry.Id.AbsoluteUri + "\r\n ");
}

// Create a new Group
GroupEntry newGroup = new GroupEntry();
newGroup.Title.Text = "New Group Title 3";
newGroup.Summary.Text = "Group's Summary 3";
newGroup = contactsService.Insert(groupUri, newGroup) as GroupEntry;

// New Group's ID
string newGroupID = newGroup.Id.AbsoluteUri;

ContactsQuery query = new ContactsQuery
( ContactsQuery.CreateContactsUri("default"));
ContactsFeed feed = contactsService.Query(query);
foreach (ContactEntry entry in feed.Entries)
{
// new Group membership element, is use to add the new group to each
user.
GroupMembership groupMembership = new GroupMembership();
groupMembership.HRef = newGroupID;
entry.GroupMembership.Add(groupMembership);
entry.Update();

Frank Mantek

unread,
Feb 26, 2009, 5:25:44 AM2/26/09
to google-co...@googlegroups.com
Or, you use the new and improved contacts object model that is
currently only available over subversion. You find this and more code
like it in src/unittests/contactstest.cs. Removed code here that is
not relevant. This method gets' the groups feed, inserst 2 groups,
creates a contact, inserts the contact, adds the contact to groups,
removes it and deletes the whole thing again...


[Test] public void GroupsModelTest()
{
RequestSettings rs = new
RequestSettings(this.ApplicationName, this.userName, this.passWord);
rs.AutoPaging = true;

ContactsRequest cr = new ContactsRequest(rs);

Feed<Group> fg = cr.GetGroups();

Group newGroup = new Group();
newGroup.Title = "Private Data";

Group insertedGroup = cr.Insert(fg, newGroup);

Group g2 = new Group();
g2.Title = "Another private Group";
Group insertedGroup2 = cr.Insert(fg, g2);


// now insert a new contact that belongs to that group
Feed<Contact> fc = cr.GetContacts();
Contact c = new Contact();
/// set up contat properties

Contact insertedEntry = cr.Insert(fc, c);

GroupMembership member = new GroupMembership();
member.HRef = insertedGroup.Id;

GroupMembership member2 = new GroupMembership();
member2.HRef = insertedGroup2.Id;

// now change the group membership
insertedEntry.GroupMembership.Add(member);
insertedEntry.GroupMembership.Add(member2);
Contact currentEntry = cr.Update(insertedEntry);

Assert.IsTrue(currentEntry.GroupMembership.Count == 2,
"The entry should be in 2 groups");

currentEntry.GroupMembership.Clear();
currentEntry = cr.Update(currentEntry);
Assert.IsTrue(currentEntry.GroupMembership.Count == 0,
"The entry should not be in groups");

cr.Delete(currentEntry);
cr.Delete(insertedGroup);
cr.Delete(insertedGroup2);

}


Frank
Reply all
Reply to author
Forward
0 new messages