Google Sheets with C# 19

Please Share

Google spead sheetsAccessing Google Sheets with C# after the Google client login shut down is not as easy as you might think.    The problem is that the Google Sheets is part of the old Gdata api and becouse of that you have to use the Gdata library to access it.   The current Google .net client library which supports Oauth2 and Service accounts only supports the newer discovery APIs.   That doesnt mean that it cant be done it just means that you need to merge two diffrent client librarys in the same project to get it to work.

This tutorial is not complte it is a work in progress, please come with feed back so that we can improve it.

 NuGet Packages

You will need the NuGet packages from both client libraries.   The first one is the current Google .net client library which we need to authentcate.   The second is the old Gdata library which we use to access Google SpreadSheets.

PM> Install-Package Google.Apis.Auth
PM> Install-Package Google.GData.Spreadsheets

The Code

Again this is a work in progress I will fix this as I get it working. It should be possible to use OAuth2 and a public API key as well I just haven’t had time to get it working. This should give you a head start if you are like me trying to access Google Sheets with C#.

Please Share

Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as a developer and a database expert. I have been working with several of the Google APIs, since 2012. I helping others in the On-Line community to develop with the Google APIs, by creating my own blog This and my presence on a number of On-Line developer forums lead me to be noticed by the Google Analytics API development team. I was nominated for and recently became one of the first Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *

19 thoughts on “Google Sheets with C#

  • Dave

    Hi Linda. Thank you for your effort.

    I am a newbie to Google API. I have followed your example. The program run smooth but no spreadsheets can be retrieved, i.e. feed.Entries.Count is equal to 0. Is there any setting i have missed ? Many Thanks.

  • Preguntón Cojonero Cabrón

    Great post!. marvellous.

    For extending it, my case is:

    I want get columns-rows of a public Spreedsheet, not mine

    For example:

    I try this, but not solution, only get errors:

    var docURL = “”;

    //docURL = “”;

    //docURL = “”;

    var queryByUrl = new SpreadsheetQuery(docURL);
    WorksheetQuery query2 = new WorksheetQuery(docURL);

    // Make a request to the API and get all spreadsheets.
    feed = service.Query(queryByUrl);
    if (feed.Entries.Count == 0)
    Console.WriteLine(“***** There are no sheets”);

    WorksheetFeed feed2a = service.Query(query2);

    if (feed2a == null)
    Console.WriteLine(“***** There are no WorksheetFeed”);

    foreach (WorksheetEntry worksheet in feed2a.Entries)


  • Peter

    Hi Linda, I just wanted to thank you very much for all the efforts you put into exploring the Google API and explaining it. It is very much appreciated!!!
    On a sidenote to this: I put the 2 codes together and did not have any trouble, I just used the ‘new’ lib to get the authentication, took the AccessToken from the credential class and passed it over to the ‘old’ lib. It was a quick and dirty POC so no code to publish, but from what I can see from these first tests, it should work fine.
    Best Regards

  • haze

    Thank you!

    Note for other users that are new to this:

    Add your using statements:
    using Google.GData.Client;
    using Google.GData.Spreadsheets;
    using Google.Apis.Auth.OAuth2;
    using System.Security.Cryptography.X509Certificates;

    After you create your Service Account, make sure you grant it access to SpreadSheet(s).

  • John Lee

    Hi I tried searching the web for a way to access google sheets using c# and I think your method is closed to the direction I was heading.
    A tutorial I read wanted me to copy and paste the access code every time the application accessed google sheets. I had to find a way to pass the access code from google to the application automatically. But I couldn’t figure out a good method for it.
    Anyways I’m trying to use your method but I’m not sure how to make the first line work for me. Not sure what type of certificate file you’re using.
    var certificate = new X509Certificate2(@”c:\Diamto Test Everything Project.p12″, “notasecret”, X509KeyStorageFlags.Exportable);

    Thanks for helping.

  • Eva

    Hi Linda,
    Awesome post! Thank you so much for these explanations. I got a problem, could you please help me with it? I have followed example: , the program ran perfectly on WebApplication, but with the same code not working on windows service. When I’m debugging it vs says that it can’t find GoogleClientSecrets.cs and then NewtonsoftJsonSerializer.cs not found. I couldn’t figure out what happened. Did I miss something? Thank you for helping.

    • Linda Lawton
      Linda Lawton Post author

      sounds like an issue with the dlls. Make sure you have all the referenced libraries set to copy local. Windows services can cause issues also depending upon which user they are running as it needs to have permissions to access the files.