1

我使用Google API NET創建了一個Google電子表格,如下所示。我沒有收到任何例外,從代碼我也能夠檢索文件ID。但是,當我看着谷歌驅動器,我沒有看到文件。有什麼我應該做的,以通知谷歌驅動器。我也刷新了我的驅動器。但沒用。任何想法?新電子表格在使用.NET API的谷歌驅動器中不可見

我驗證我的服務如下圖所示:

authenticate.cs

public class SerivceAccount 
    { 
     private const string ServiceAccountEmail = "[email protected]"; 
     private static readonly X509Certificate2 Certificate = new X509Certificate2("ADExpress.p12", "notasecret", X509KeyStorageFlags.Exportable); 

     readonly ServiceAccountCredential _credential = new ServiceAccountCredential(
       new ServiceAccountCredential.Initializer(ServiceAccountEmail) 
       { 
        Scopes = new[] 
        { 
         "https://spreadsheets.google.com/feeds", 
         DriveService.Scope.Drive, 
         "https://www.googleapis.com/auth/drive.file" 
        } 
       }.FromCertificate(Certificate)); 

     public bool Authenticate() 
     { 
      var isAuthenticated = false; 
      try 
      { 
       if (!_credential.RequestAccessTokenAsync(System.Threading.CancellationToken.None).Result) 
        throw new InvalidOperationException("Access token request failed."); 
       isAuthenticated = true; 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine("Exception in Authenticating " + e.ToString()); 
      } 

      return isAuthenticated; 
     } 

     public ServiceAccountCredential Credential 
     { 
      get { return _credential; } 
     } 

    } 

main.cs

var account = new SerivceAccount(); 
if (account.Authenticate()) 
      { 
    FilesResource.InsertRequest request = service.Files.Insert(new Google.Apis.Drive.v2.Data.File() 
         { 
          Title = "Test", 
          Description = "Test", 
          MimeType = "application/vnd.google-apps.spreadsheet" 
         }); 
         var file = request.Execute(); 
Console.WriteLine("File ID: " + file.Id); 
} 
+0

服務如何驗證? – DaImTo

+0

@DalmTo,我用身份驗證請求更新了我的問題。 –

回答

0

您正在使用服務帳戶進行身份驗證。

我做了刷新我的車開得

檢查你自己的谷歌雲端硬盤網絡版不會告訴你,凡法服務帳戶創建的文件。服務帳戶是它自己的用戶。可能正在對服務創建您的文件佔谷歌硬盤帳戶,你可以通過做files.list

例如測試:

/// <summary> 
    /// Retrieve a list of File resources. 
    /// </summary> 
    /// <param name="service">Drive API service instance.</param> 
    /// <returns>List of File resources.</returns> 
    public static List<File> retrieveAllFiles(DriveService service) { 
    List<File> result = new List<File>(); 
    FilesResource.ListRequest request = service.Files.List(); 

    do { 
     try { 
     FileList files = request.Execute(); 

     result.AddRange(files.Items); 
     request.PageToken = files.NextPageToken; 
     } catch (Exception e) { 
     Console.WriteLine("An error occurred: " + e.Message); 
     request.PageToken = null; 
     } 
    } while (!String.IsNullOrEmpty(request.PageToken)); 
    return result; 
    } 

爲了讓其他用戶查看文件,您必須授予權限請參閱由服務帳戶創建的文件。

0

其實這有助於解決我的問題。我需要提供文件的權限才能在UI界面中顯示。希望這可以幫助。

Permission newPermission = new Permission(); 
newPermission.Value = "[email protected]"; 
newPermission.Type = "user"; 
newPermission.Role = "reader"; 
service.Permissions.Insert(newPermission, file.Id).Execute(); 
相關問題