Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# application - Read Google Contacts

Since Google stopped support for their older Auth, and now we have to use oAuth 2, our simple desktop application can no longer read contacts from my google account.

Fine - i understand this, however this new oAuth 2 is extraordinarily complicated... and im not talking about from a developer perspective. From what i am reading online. We now have to make our customers jump over a multitude of hoops in order for our simple application to read contacts stored in their Google mail/Contacts.

My iPhone seems to be able to sync contacts just fine with just the typical email and password that i entered about a year ago. How do they get it to work? and yet with my simple desktop application, the client has to be rummaging around in Google Developer sites and with API settings etc. I'm a developer and im confused!! - could you imagine what my customer is going to go through... it cant be this complicated.

Is there anyone who can give me the simple 1,2,3 to get a C# desktop application to go off and get the contacts (read-only) from a particular Gmail account... with the least amount of fiddling around (for the owner of the Gmail account).

Ill do all the hard work in the application - i just don't want to client to have to spend an hour authorizing and creating API's and clicking around in a developer site (he/she is NOT a developer).

like image 926
Conrad de Wet Avatar asked Oct 27 '15 08:10

Conrad de Wet


1 Answers

The main problem you have here is that contacts is an old Gdata API. It is possible to use Oauth2 with the Gdata library but its not pretty. Personally I like to hack things a little. I use the Current .net client library with the old Gdata client library.

Nuget New client library for authentication:

not 100% sure this is the only one you need let me know if it doesn't work we can find it. You basically need Google.apis.auth.oauth2 and google apis.util.store.

Install-Package Google.Apis.Auth

Nuget old client library for contacts:

Install-Package Google.GData.Contacts

Code

 using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Contacts;
using Google.GData.Client;
using System;
using System.Threading;

public static void auth()
{

    string clientId = "xxxxxx.apps.googleusercontent.com";
    string clientSecret = "xxxxx";


    string[] scopes = new string[] { "https://www.googleapis.com/auth/contacts.readonly" };     // view your basic profile info.
    try
    {
        // Use the current Google .net client library to get the Oauth2 stuff.
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                     , scopes
                                                                                     , "test"
                                                                                     , CancellationToken.None
                                                                                     , new FileDataStore("test")).Result;

        // Translate the Oauth permissions to something the old client libray can read
        OAuth2Parameters parameters = new OAuth2Parameters();
        parameters.AccessToken = credential.Token.AccessToken;
        parameters.RefreshToken = credential.Token.RefreshToken;
        RunContactsSample(parameters);
        Console.ReadLine();
    }
    catch (Exception ex)
    {

        Console.ReadLine();

    }
    Console.ReadLine();


}

/// <summary> 
/// Send authorized queries to a Request-based library 
/// </summary> 
/// <param name="service"></param> 
private static void RunContactsSample(OAuth2Parameters parameters)
{
    try
    {
        RequestSettings settings = new RequestSettings("Google contacts tutorial", parameters);
        ContactsRequest cr = new ContactsRequest(settings);
        Feed<Contact> f = cr.GetContacts();
        foreach (Contact c in f.Entries)
        {
            Console.WriteLine(c.Name.FullName);
        }
    }
    catch (Exception a)
    {
        Console.WriteLine("A Google Apps error occurred.");
        Console.WriteLine();
    }
}

Tutorial can be found here

Google developers console

All applications accessing google apis must be registered on Google developers console. It is the application accessing Google that is registered users running the code do not need to do this step. Its you as a developer who has to register it.

From this you get the client id and client secret used in the code above.

like image 143
DaImTo Avatar answered Oct 15 '22 15:10

DaImTo