Barakr
Barakr

Reputation: 59

Google drive API to C#

Hi I am trying to read a spreadsheet using google drive API using the google api example.

however I getting the same failure: "An unhandled exception of type 'Google.GData.Client.GDataRequestException' occurred in Google.GData.Client.dll

Additional information: Execution of authentication request returned unexpected result: 404"

I am sure I am entering the right login and password.

here is the code:

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        SpreadsheetsService myService = new SpreadsheetsService("exampleCo-exampleApp-1");
        myService.setUserCredentials(myusername, password);            
        SpreadsheetQuery query = new SpreadsheetQuery();
        SpreadsheetFeed feed = myService.Query(query);

        Console.WriteLine("Your spreadsheets:");
        foreach (SpreadsheetEntry entry in feed.Entries)
        {
            Console.WriteLine(entry.Title.Text);
        }

    }
}

Upvotes: 1

Views: 668

Answers (1)

Josh T.
Josh T.

Reputation: 171

The old authentication methods in the GData API have been deprecated. You need to use OAuth2.


This example requires you to use the following nuget packages and their dependencies:

  • Google.GData.Spreadsheets

Also, you must go to https://console.developers.google.com and register your application and create credentials for it so you can enter your CLIENT_ID and CLIENT_SECRET.

This is the documentation I used to put together this example: https://developers.google.com/google-apps/spreadsheets/

using System;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Spreadsheets;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string CLIENT_ID = "YOUR_CLIENT_ID";
            string CLIENT_SECRET = "YOUR_CLIENT_SECRET";
            string SCOPE = "https://spreadsheets.google.com/feeds https://docs.google.com/feeds";
            string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

            OAuth2Parameters parameters = new OAuth2Parameters();

            parameters.ClientId = CLIENT_ID;
            parameters.ClientSecret = CLIENT_SECRET;
            parameters.RedirectUri = REDIRECT_URI;
            parameters.Scope = SCOPE;

            string authorizationUrl = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters);
            MessageBox.Show(authorizationUrl);
            Console.WriteLine("Please visit the URL in the message box to authorize your OAuth "
              + "request token.  Once that is complete, type in your access code to "
              + "continue...");
            parameters.AccessCode = Console.ReadLine();

            OAuthUtil.GetAccessToken(parameters);
            string accessToken = parameters.AccessToken;
            Console.WriteLine("OAuth Access Token: " + accessToken);

            GOAuth2RequestFactory requestFactory =
                new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
            SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
            service.RequestFactory = requestFactory;

            SpreadsheetQuery query = new SpreadsheetQuery();

            SpreadsheetFeed feed = service.Query(query);

            // Iterate through all of the spreadsheets returned
            foreach (SpreadsheetEntry entry in feed.Entries)
            {
                // Print the title of this spreadsheet to the screen
                Console.WriteLine(entry.Title.Text);
            }
            Console.ReadLine();
        }
    }
}

Upvotes: 2

Related Questions