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.
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);
}
}
}
See more on this question at Stackoverflow