找不到我使用谷歌驱动器上传到谷歌驱动器的文件sdk

问题描述:

我正在使用谷歌驱动器的.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

如果不工作,你可以尝试在此related SO question建议的答案:

由于您使用的是服务帐户,所有文件夹和文件将被创建在此服务帐户的云端硬盘中,无法通过网络用户界面访问此云端硬盘并将其限制为默认配额。

要在用户的云端硬盘中添加内容,您需要通过常规的OAuth 2.0流程来检索此用户的凭据。你可以找到这个网页有关OAuth 2.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]我现在可以看到文件登录到谷歌驱动