I am trying download file from google drive using the code below:
public static Boolean downloadFile(string downloadurl, string _saveTo)
{
if (!String.IsNullOrEmpty(downloadurl))
{
try
{
var x = service.HttpClient.GetByteArrayAsync(downloadurl);
byte[] arrBytes = x.Result;
System.IO.File.WriteAllBytes(_saveTo, arrBytes);
return true;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return false;
}
}
else
{
// The file doesn't have any content stored on Drive.
return false;
}
}
On debugging the above code throwing exception as below:
?service.HttpClient.GetByteArrayAsync(downloadurl)
Id = 10, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"
AsyncState: null
CancellationPending: false
CreationOptions: None
Exception: null
Id: 10
Result: null
Status: WaitingForActivation
I am trying to do it from my Service account created using Google API Console.
and the exception detail is as follows:
System.NullReferenceException was caught
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=System.Net.Http
StackTrace:
at System.Net.Http.Headers.HttpRequestHeaders.AddHeaders(HttpHeaders sourceHeaders)
at System.Net.Http.HttpClient.PrepareRequestMessage(HttpRequestMessage request)
at System.Net.Http.HttpClient.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.GetAsync(Uri requestUri, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.GetContentAsync[T](Uri requestUri, HttpCompletionOption completionOption, T defaultValue, Func`2 readAs)
at System.Net.Http.HttpClient.GetByteArrayAsync(Uri requestUri)
at System.Net.Http.HttpClient.GetByteArrayAsync(String requestUri)