2017-08-28 69 views
0

我運行這段代碼:上傳文件,以推動使用谷歌驅動的API和C#

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; 


    } 
} 

,並出現以下情況例外:

enter image description here

當我調用該函數: GetUserCredential();

任何想法會導致錯誤?

我實際上是通過單詞從視頻單詞中複製代碼,我看不到該錯誤的任何邏輯原因。我使用的視頻:https://www.youtube.com/watch?v=nhQPwrrJ9kI&t=259s

+1

可能重複[什麼是NullReferenceException,以及如何解決它?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix -it) –

+0

你檢查過GoogleWebAuthorizationBroker是否爲空? –

回答

1

這是失敗的:

GoogleClientSecrets.Load(stream).Secrets 

您可以使用以下說明生成該文件,如果你沒有它:https://github.com/jay0lee/GAM/wiki/CreatingClientSecretsFile

確保文件client_se cret.json在您的應用程序文件夾(其中的EXE)存在,然後:

using (var stream = new FileStream(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, 
      "client_secret.json"), FileMode.OpenOrCreate, FileAccess.Read)) 

當然,不包括這個文件部署應用程序時,每個用戶都必須和自己的文件複製到該應用程序夾。

1

請仔細檢查以下鏈接如何獲取用戶憑證。

OAuth 2.0 - Credentials

這是代碼示例:

private async Task Run() 
    { 
     UserCredential credential; 
     using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)) 
     { 
      credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
       GoogleClientSecrets.Load(stream).Secrets, 
       new[] { BooksService.Scope.Books }, 
       "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary")); 
     } 

     // Create the service. 
     var service = new BooksService(new BaseClientService.Initializer() 
      { 
       HttpClientInitializer = credential, 
       ApplicationName = "Books API Sample", 
      }); 

     var bookshelves = await service.Mylibrary.Bookshelves.List().ExecuteAsync(); 
     ... 
    } 
相關問題