Search code examples
c#gdata-api

How do I request a single Spreadsheet with the C# GData API by Key?


I have a program that authenticates and can fetch a list of all spreadsheets that uses version 3 of the C# implementation of the the GData API (it follows the the example found in the documentation). What I am having a hard time figuring out is how to fetch a single resource by it's document key. I can see plenty of examples of retrieving lists of documents in the Google Documents List API and I can see how to zero into a specific document by an exact name search, but I'm trying to produce some code that survives a Spreadsheet being renamed. Here is the code I've written so far:

using System;
using Google.GData.Client;
using Google.GData.Spreadsheets;
using Microsoft.VisualBasic;

namespace q14201622
{
  class Program
  {
    static void Main(string[] args)
    {
      //OAuth 2 Stuff
      var parameters = new OAuth2Parameters() {
        ClientId = CLIENT_ID,
        ClientSecret = CLIENT_SECRET,
        RedirectUri = REDIRECT_URI,
        Scope = SCOPE
      };
      Process.Start(OAuthUtil.CreateOAuth2AuthorizationUrl(parameters));
      var response = Interaction.InputBox("Enter oAuth Code:", "q14201622", "", 0, 0);
      parameters.AccessCode = response;
      OAuthUtil.GetAccessToken(parameters);
      var accessToken = parameters.AccessToken;

      //Fetch Documents
      var requestFactory = new GOAuth2RequestFactory(null, "q14201622-v1", parameters);
      var service = new SpreadsheetsService("q14201622-v1");
      var query = new SpreadsheetQuery();
      query.Title = "Exact Title";
      query.Exact = true;

      //Iterate over the results
      var feed = service.Query(query);
      foreach (var entry in feed.Entries)
      {
        Console.WriteLine(entry.Id + ":"+ entry.Title.Text);
      }
    }
  }
}

Solution

  • I did not test this, but according to this https://developers.google.com/gdata/client-cs you can request a specific entry by using its URL :

    FeedQuery singleQuery = new FeedQuery();
    singleQuery.Uri = new Uri(newEntry.SelfUri.ToString()); 
    AtomFeed newFeed = service.Query(singleQuery);
    AtomEntry retrievedEntry = newFeed.Entries[0];    
    

    Now, if you have the document key, you also have the URL (https://docs.google.com/spreadsheet/ccc?key=[insert key here]) you can use a simple

    string docKey = "nice doc key";
    string  gDocsURL = "https://docs.google.com/spreadsheet/ccc?key={0}";
    string docURL = String.Format(gDocsURL,docKey);
    

    and then

    FeedQuery singleQuery = new FeedQuery();
    singleQuery.Uri = new Uri(docURL); 
    AtomFeed newFeed = service.Query(singleQuery);
    AtomEntry retrievedEntry = newFeed.Entries[0];  
    

    And you should be able to retrieve the doc. If you want to save the doc to the HDD, simply add

    string docFormat ="xlsx";// or xls or csv or pdf etc.
    string gDocsDownloadURL="https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key={0}&exportFormat={1}"
    string downloadUrl = String.format(gDocsDownloadURL,docKey,docFormat);
    Stream stream = service.Query(new Uri(downloadUrl));