Search code examples
c#.netgoogle-apigoogle-oauthgoogle-contacts-api

Google Contacts API


I just started working with the google contacts api and can´t find an example how to work with the authentification stuff.

I used the Google Contacts API version 3.0 documentation for understanding the basic workflow with the contacts API, but I have no idea how to work with the authentification tokens.

After some searching in the web I found the tutorial Google OAuth2 C# but in this tutorial they are working with the UserCredential object. In the Google Contacts API version 3.0 documentation they used the RequestSettings object. This object has a constructor that accepts a ApplicationName and a GDataCredentials object.

So I tried the following code:

GDataCrendentials credentials = new GDataCredentials(CLIENTID);
credentials.Username = "<my gmail username>";
this._requestSettings = new RequestSettings(GetApplicationName, _credentials);

The ClientId I got from the second link I posted.

So I tried to access my contacts with the code:

Feed<Google.Contacts.Contact> f = _contactsRequest.GetContacts();
IList<IContact> mappedContacts = new List<IContact>();

foreach (var contact in f.Entries)
{
    //do some stuff with it
}

With this code I get an Google.GData.Client.GDataRequestException at the foreach:

{"Execution of request failed: https://www.google.com/m8/feeds/contacts/default/full"}

Could you please give me a suggestion what is wrong here?


Solution

  • There is an OAuth2 sample included with the GData .NET client library:

    https://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/samples/oauth2_sample/oauth2demo.cs

    The relevant source code is located here:

    https://code.google.com/p/google-gdata/source/browse/trunk/clients/cs/src/core/oauthutil.cs#200