I'm trying to connect to my GoogleDrive using C#. My code is:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar.v3;
using Google.Apis.Drive.v3;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
public class GoogleDriveFiles
{
public string Id { get; set; }
public string Name { get; set; }
public long? Size { get; set; }
public long? Version { get; set; }
public DateTime? CreatedTime { get; set; }
}
static string ApplicationName = "test";
static string[] Scopes = { CalendarService.Scope.Calendar };
string credentialsJsonFIle = "c:\\webroot\\docs\\googleDriveCredentials.json";
[Obsolete]
protected void Page_Load(object sender, EventArgs e)
{
UserCredential credential;
using (var stream = new FileStream(credentialsJsonFIle, FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = @"\token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// 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.PageSize = int.MaxValue;
listRequest.Fields = "nextPageToken, files(id, name, parents, size, shared, fullFileExtension, fileExtension, version, createdTime)";
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;
Response.Write("Files:<br/>");
FilesResource.ListRequest FileListRequest = service.Files.List();
//get file list.
List<GoogleDriveFiles> FileList = new List<GoogleDriveFiles>();
string tempPath = Path.GetTempPath();
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
if (file.FileExtension == "pdf")
{
GoogleDriveFiles File = new GoogleDriveFiles
{
Id = file.Id,
Name = file.Name,
Size = file.Size,
Version = file.Version,
CreatedTime = file.CreatedTime
};
FileList.Add(File);
FilesResource.GetRequest request = service.Files.Get(file.Id);
MemoryStream stream1 = new MemoryStream();
string pathFile = System.IO.Path.Combine(tempPath, file.Name);
request.Download(stream1);
SaveStream(stream1, pathFile);
service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
}
}
}
else
{
Response.Write("No files found.<br/>");
}
}
And I'm getting the following error message: The network location cannot be reached. For information about network troubleshooting, see Windows Help
At line: credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
My stacktrace displays the following:
[HttpListenerException (0x4d0): The network location cannot be reached. For information about network troubleshooting, see Windows Help]
System.Net.HttpListener.AddAllPrefixes() +352
System.Net.HttpListener.Start() +297
Google.Apis.Auth.OAuth2.LocalServerCodeReceiver.StartListener() +114
Google.Apis.Auth.OAuth2.<ReceiveCodeAsync>d__13.MoveNext() +76
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Google.Apis.Auth.OAuth2.<AuthorizeAsync>d__8.MoveNext() +479
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Google.Apis.Auth.OAuth2.<AuthorizeAsync>d__4.MoveNext() +422
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +99
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58
Google.Apis.Auth.OAuth2.<AuthorizeAsync>d__1.MoveNext() +286
[AggregateException: One or more errors occurred.]
System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) +4323141
System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) +12865987
System.Threading.Tasks.Task`1.get_Result() +33
TestGoogleDrive.Page_Load(Object sender, EventArgs e) in c:\Webroot\www.godigix.com\test\testGoogleDrive.aspx.cs:48
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +95
System.Web.UI.Control.LoadRecursive() +59
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678
Can someone help me? My json file path is correct and file exists at given path.