2017-03-10 99 views
0

嘗試創建文件夾並設置其權限時出現以下錯誤。它將在不傳遞文件元數據權限的情況下工作。在Google Drive API中設置文件夾權限

但我想創建具有特定用戶權限的文件夾,每個用戶都有自己的g-mail帳戶。我怎樣才能做到這一點?謝謝!

錯誤消息:

Exception message upon compiling/executing

驗證碼:

using Google.Apis.Auth.OAuth2; 
using Google.Apis.Drive.v3; 
using Google.Apis.Drive.v3.Data; 
using Google.Apis.Services; 
using Google.Apis.Util.Store; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 


namespace DriveQuickstart 
{ 
    class Program 
    { 
    // If modifying these scopes, delete your previously saved credentials 
    // at ~/.credentials/drive-dotnet-quickstart.json 
    static string[] Scopes = { DriveService.Scope.Drive }; 
    static string ApplicationName = "Drive API .NET Quickstart"; 

    static void Main(string[] args) 
    { 
     UserCredential credential; 

     using (var stream = 
     new FileStream("client_secret.json", FileMode.Open, FileAccess.ReadWrite)) 
     { 
     string credPath = System.Environment.GetFolderPath(
     System.Environment.SpecialFolder.Personal); 

     credPath = Path.Combine(credPath, ".credentials/drive-dotnet-quickstart.json"); 

     credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
     GoogleClientSecrets.Load(stream).Secrets, 
     Scopes, 
     "user", 
     CancellationToken.None, 
     new FileDataStore(credPath, true)).Result; 
     Console.WriteLine("Credential file saved to: " + credPath); 
     } 

     // Create Drive API service. 
     var service = new DriveService(new BaseClientService.Initializer() 
     { 
     HttpClientInitializer = credential, 
     ApplicationName = ApplicationName, 
     }); 

     Permission perm = new Permission(); 

     perm.Type = "user"; 
     perm.EmailAddress = "[email protected]"; 
     perm.Role = "owner"; 

     IList<Permission> perms = new List<Permission>(); 

     perms.Add(perm); 

     var fileMetadata = new Google.Apis.Drive.v3.Data.File() 
     { 
     Name = "Invoices", 
     MimeType = "application/vnd.google-apps.folder", 
     Description = "Blah blah " + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second, 
     Permissions = perms 
     }; 

     var request = service.Files.Create(fileMetadata); 
     request.Fields = "id"; 
     var file = request.Execute(); 
     Console.WriteLine("Folder ID: " + file.Id); 

     Console.Read(); 

    } 
    } 
} 

回答

2

正如你可以在Documentation看到,該Permissions[]屬性表示爲writable場。因此,在文件創建之前在資源體中填充Permissions[]並不是正確的方法;就像錯誤表示「資源體包含不可直接寫入的字段」。

那麼如何創建共享文件夾呢?

您必須先使用其Id創建該文件夾,然後Create the Permission

您可以修改你的代碼:

var fileMetadata = new Google.Apis.Drive.v3.Data.File() 
{ 
    Name = "Invoices", 
    MimeType = "application/vnd.google-apps.folder", 
    Description = "Blah blah " + System.DateTime.Now.Hour + ":" + System.DateTime.Now.Minute + ":" + System.DateTime.Now.Second 
}; 

Permission perm = new Permission(); 
perm.Type = "user"; 
perm.EmailAddress = "[email protected]"; 
perm.Role = "owner"; 

var request = service.Files.Create(fileMetadata); 
request.Fields = "id"; 

try 
{ 
    service.Permissions.Create(perm, request.Execute().Id).Execute(); //Creating Permission after folder creation. 
} 
catch (Exception e) 
{ 
    Console.WriteLine("An error occurred: " + e.Message); 
} 
1

確保您正確驗證。看到這個link

當您想要訪問Google Drive API時,身份驗證是一切的關鍵。如果您希望能夠訪問數據,則需要進行身份驗證。有兩種身份驗證OAuth2可供您訪問其他用戶數據以及一個可設置爲訪問自己數據的服務帳戶。

您可以按照本教程中的示例代碼:Google Drive file permissions

/// <summary> 
    /// Insert a new permission. 
    /// </summary> 
    /// <param name="service">Drive API service instance.</param> 
    /// <param name="fileId">ID of the file to insert permission for.</param> 
    /// <param name="who"> 
    /// User or group e-mail address, domain name or null for "default" type. 
    /// </param> 
    /// <param name="type">The value "user", "group", "domain" or "default".</param> 
    /// <param name="role">The value "owner", "writer" or "reader".</param> 
    /// <returns>The inserted permission, null is returned if an API error occurred</returns> 
    public static Permission InsertPermission(DriveService service, String fileId, String who,String type, String role) { 
    Permission newPermission = new Permission(); 
    newPermission.Value = value; 
    newPermission.Type = type; 
    newPermission.Role = role; 
    try { 
     return service.Permissions.Insert(newPermission, fileId).Execute(); 
    } catch (Exception e) { 
     Console.WriteLine("An error occurred: " + e.Message); 
    } 
    return null; 
    }