I am wanting to make a web application which uses the Google Drive API to access a friend's spreadsheet located on his Google Drive. It will be reading/writing to the sheet but nothing more, and only that sheet. Suffice to say, I have spent the last two days reading up documentation for the Google Drive API and have little to show for it. I just can't make sense of it, no matter how many examples I read because the API doesn't match what the examples show. There must be 4-5 different ways that I found of authentication with OAuth2.0 but none will work with the Drive API that I got off the NuGet package manager (which is where Google said to get the latest API). Nothing makes sense. The OAuth2Parameters class doesn't exist so I don't know how to authenticate with the examples google gives. Similar ways end with the same result with other examples, like Google's DrEdit. Can anyone provide some insight? The spreadsheet will be hosted by my friend (me if necessary) and the web application will be hosted by a free service, say AppHarbor. Thank you very much.
Asked
Active
Viewed 6,445 times
1

abatishchev
- 98,240
- 88
- 296
- 433

dangerisgo
- 1,261
- 3
- 16
- 28
-
I feel your pain, the Google docs are the worst I've ever seen and rarely maintained, but this isn't the format for this kind of question - it's too broad. Get the Google .NET Client Library from Nuget and likewise the Drive SDK for it. Then look at the docs on the .NET Client Library Google Code website. They're much better than the official so-called "docs" – CodingIntrigue Oct 18 '14 at 07:17
-
If you are referring to: https://code.google.com/p/google-api-dotnet-client/ , then I'm afraid all the documentation has been moved to the page I was just looking at before...the one that doesn't make sense and is out of date. – dangerisgo Oct 18 '14 at 07:26
-
to update a spreadsheet, there are 2 options: 1) google-apps-script (does not require Oauth2) 2) google-sparesheet-api https://developers.google.com/google-apps/spreadsheets/ ---- yes the OAuth2 stuff in a pain, everyone looks to struggle with it. I extracted my OAuth2 code from DrEdit, very messy, but worked. – eddyparkinson Oct 19 '14 at 01:14
-
Succinctly, if you need to do operations on the spreadsheet itself (data inport/export, formatting, charts, etc.), use the Google Sheets API (http://developers.google.com/sheets), and if you're working with Sheets at the file level, then use the Google Drive API (http://developers.google.com/drive). If you're still confused about auth and using Google APIs, check all the video & reading resources I posted in this SO answer for clarity: http://stackoverflow.com/a/38359875/305689 (most code is in Python so mentally translate to C#) – wescpy Jul 15 '16 at 23:59
1 Answers
6
I've just tried the code in the quickstart guide and it worked without any problems at all, using the Google.Apis.Drive.v2
nuget package. As you say, there are lots of different approaches to auth and the code in the quckstart isn't appropriate for a web application, but they should all be available with the latest SDKs. See the web server authorization page for details.
I think the OAuth2Parameters
class belongs to the "old" gdata approach to Google APIs; samples using it are probably for the older version of the Drive SDK.
I didn't want to create a file in my test, so I adapted the sample code to just list files:
using System;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Services;
class Test
{
static void Main(string[] args)
{
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = /* YOUR CLIENT ID HERE */,
ClientSecret = /* YOUR CLIENT SECRET HERE */,
},
new[] { DriveService.Scope.Drive },
/* USER TO AUTHORIZE */,
CancellationToken.None).Result;
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Stack Overflow Drive test",
});
var response = service.Files.List().Execute();
foreach (var item in response.Items)
{
Console.WriteLine(item.Title);
}
}
}

Jon Skeet
- 1,421,763
- 867
- 9,128
- 9,194
-
Thank you for your reply. In my case,
would be what? Would it be a username? An Access API? – dangerisgo Oct 18 '14 at 07:46 -
@dangerisgo: Well I set it to my email address... I confess it's not entirely clear to me how this corresponds to the user choice I get in the browser - but if you're writing a web app anyway, that part won't be as relevant. It's a good starting point to check that everything's working though. – Jon Skeet Oct 18 '14 at 07:58
-
This is an excellent starting point for my web app. Thank you very much for your help Jon! – dangerisgo Oct 18 '14 at 08:10