Im trying to learn how to use the google API and I've started with a sample that can be found here: https://developers.google.com/drive/web/quickstart/dotnet
I believe I've followed the instructions exactly and I've copy pasted all the code, but I cant get it to work.
If I debug it Visual Studio ask me for the location of GoogleClientSecrets.cs when I reach the GoogleWebAuthorizationBroker.AuthorizeAsync method.
If I just continue the code throws an HttpListenerException with the message (translated from Swedish) "Supplied network name has wrong format". The error code is 1214.
I guess it should open the browser at this point, but it doesnt, could that be the issue?
This is all new to me, so Im have a hard time finding a solution. I've been googleling for a solution for quite some time now.
Any suggestions would be very much appriciated.
This is the code in its entirety:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace DriveQuickstart
{
class Program
{
static string[] Scopes = { DriveService.Scope.DriveReadonly };
static string ApplicationName = "Drive API Quickstart";
static void Main(string[] args)
{
UserCredential credential;
using (var stream =
new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Drive API service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.MaxResults = 10;
// List files.
IList<Google.Apis.Drive.v2.Data.File> files = listRequest.Execute()
.Items;
Console.WriteLine("Files:");
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
Console.WriteLine("{0} ({1})", file.Title, file.Id);
}
}
else
{
Console.WriteLine("No files found.");
}
Console.Read();
}
}
}
答案 0 :(得分:0)
好吧,我没有回答为什么这不起作用,但我找到了另一种方法:跳过Drive API并转到GData api。
可以在这里找到一个完美无瑕的样本: https://developers.google.com/google-apps/spreadsheets/?hl=en