i am running this code:
private static string[] Scopes = { DriveService.Scope.DriveReadonly };
private static string ApplicationName = "Drive API .NET Quickstart";
private static string _folderId = "0B3s20ZXnD-M7dkdhZzZaeXZRTWc";
private static string _fileName = "testFile";
private static string _filePath = @"C:\Users\Yosi\Desktop\bla bla.rar";
private static string _contentType = "application/zip";
static void Main(string[] args)
{
Console.WriteLine("create cred");
UserCredential credential = GetUserCredential();
Console.WriteLine("Get Services");
DriveService service = GetDriveService(credential);
Console.WriteLine("Upload File");
UploadFileToDrive(service, _fileName, _filePath, _contentType);
}
private static UserCredential GetUserCredential()
{
using (var stream = new FileStream("client_secret.json", FileMode.OpenOrCreate, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json");
return GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user1",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
}
private static DriveService GetDriveService(UserCredential credential)
{
return new DriveService(
new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
}
private static string UploadFileToDrive (DriveService service, string fileName, string filePath, string conentType)
{
var fileMatadata = new Google.Apis.Drive.v3.Data.File();
fileMatadata.Name = fileName;
fileMatadata.Parents = new List<string> { _folderId };
FilesResource.CreateMediaUpload request;
using (var stream = new FileStream(filePath, FileMode.Open))
{
request = service.Files.Create(fileMatadata, stream, conentType);
request.Upload();
}
var file = request.ResponseBody;
return file.Id;
}
}
and get the following exception:
when I am calling the function: GetUserCredential();
Any ideas what can cause the error?
I actually copy the code from a video word by word and i can't see any logical reason for that error. The video I used : https://www.youtube.com/watch?v=nhQPwrrJ9kI&t=259s