2016-08-19 206 views
0

我正在使用谷歌驅動器的.NET SDK。 我已經成功設法上傳谷歌驅動器上的文件,但是當我使用瀏覽器登錄無法看到該文件嗎?找不到我使用谷歌驅動器上傳到谷歌驅動器的文件sdk

這裏是我的代碼:

private string privKey = "-----BEGIN PRIVATE KEY-----\MYKEYHERE-----END PRIVATE KEY-----\n"; 

    private string[] scopes = { DriveService.Scope.Drive }; 
    string serviceAccountEmail = "<myaccounthere>"; 

    public File uploadToDrive(string uploadFilePath) 
    { 
     ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(serviceAccountEmail) 
     { 
      Scopes = scopes 
     }.FromPrivateKey(privKey)); 

     BaseClientService.Initializer initializer = new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = credential, 
     }; 

     // Create Drive API service. 
     var service = new DriveService(initializer); 

     if (!string.IsNullOrEmpty(uploadFilePath)) 
     { 
      File fileMetadata = new File(); 
      fileMetadata.Name = System.IO.Path.GetFileName(uploadFilePath); 
      fileMetadata.MimeType = GetMimeType(uploadFilePath); 

      try 
      { 
       System.IO.FileStream uploadStream = new System.IO.FileStream(uploadFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); 
       FilesResource.CreateMediaUpload request = service.Files.Create(fileMetadata, uploadStream, GetMimeType(uploadFilePath)); 
       request.ProgressChanged += Upload_ProgressChanged; 
       request.ResponseReceived += UploadRequest_ResponseReceived; 

       var task = request.UploadAsync(); 
       task.ContinueWith(t => 
       { 

       }); 
       return request.ResponseBody; 


      } 
      catch (System.IO.IOException iox) 
      { 
       return null; 
      } 
      catch (Exception e) 
      { 
       //Log 
       return null; 
      } 
     } 
     else 
     { 
      //Log file does not exist 
      return null; 
     } 
    } 

    private string GetMimeType(string fileName) 
    { 
     string mimeType = "application/unknown"; 

     string ext = System.IO.Path.GetExtension(fileName).ToLower(); 

     Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); 

     if (regKey != null && regKey.GetValue("Content Type") != null) 
      mimeType = regKey.GetValue("Content Type").ToString(); 

     return mimeType; 
    } 

    private void Upload_ProgressChanged(IUploadProgress progress) 
    { 
     Debug.WriteLine(progress.Status + " " + progress.BytesSent); 
    } 

    private void UploadRequest_ResponseReceived(Google.Apis.Drive.v3.Data.File file) 
    { 
     Debug.WriteLine(file.Name + " was uploaded successfully"); 
    } 

有誰知道發生什麼我做錯了嗎?

在此先感謝

+0

如下面的abielita和pinoyyid突出顯示的,您的程序正在使用服務帳戶。但是,當您使用瀏覽器登錄時,您正在使用通常的帳戶。這兩個帳戶都不會在Google雲端硬盤中共享自己的一組不同的文件和文件夾,除非您明確共享它們之間的特定文件和文件夾。 – some1

回答

0

如果不工作,你可以嘗試在此related SO question建議的答案:

由於您使用的是服務帳戶,所有文件夾和文件將被創建在此服務帳戶的雲端硬盤中,無法通過網絡用戶界面訪問此雲端硬盤並將其限制爲默認配額。

要在用戶的雲端硬盤中添加內容,您需要通過常規的OAuth 2.0流程來檢索此用戶的憑據。你可以找到這個網頁有關OAuth 2.0的更多信息:

希望這有助於!

0

是的你是對的。服務帳戶有這個限制。對於任何具有相同問題的人(使用.NET)來解決這個問題的方法是啓用域範圍委派並模擬我需要的帳戶。

如我的服務帳戶[email protected]冒充[email protected]

ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer("[email protected]") 
      { 
       Scopes = new string[] { DriveService.Scope.Drive }, 
       User = "[email protected]" 
      }.FromPrivateKey(PRIVATE_KEY)); 

當我使用[email protected]我現在可以看到文件登錄到谷歌驅動

相關問題