解决方法
首先,您需要确保您具有配置了clientid / secret和正确的重定向uri的应用程序.对于我的情况,它是一个桌面应用程序:
到目前为止,您将获得clientId / secret密钥:
现在是编写一些代码的时候了!
步骤1 – 授权:
private async static Task<UserCredential> Auth(ClientSecrets clientSecrets) { return await GoogleWebAuthorizationBroker.AuthorizeAsync(clientSecrets,Scopes,"user",CancellationToken.None); }
步骤2 – 构建您的客户端的谷歌驱动器:
private static DriveService GetService(UserCredential credential) { return new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential,ApplicationName = "MyApplicationName",}); }
private static string CreateFolder(DriveService service,string folderName) { var file = new File {Title = folderName,MimeType = "application/vnd.google-apps.folder"}; var result = service.Files.Insert(file).Execute(); return result.Id; }
步骤4 – 分享吧!
/// <summary> /// Share content. Doc link: https://developers.google.com/drive/v2/reference/permissions/insert /// </summary> private static void Share(DriveService service,string fileId,string value,string type,string role) { var permission = new Permission {Value = value,Type = type,Role = role}; service.Permissions.Insert(permission,fileId).Execute(); }
最后使用整件事情:
static void Main(string[] args) { var ClientId = "MySecredId"; var SecretKey = "MySecretKey"; var Scopes = new[] { DriveService.Scope.DriveFile,DriveService.Scope.Drive }; var secrets = new ClientSecrets { ClientId = ClientId,ClientSecret = SecretKey }; var credentials = Auth(secrets).Result; var service = GetService(credentials); var folderId = CreateFolder(service,"folderName"); Share(service,folderId,"user@gmail.com","reader"); }
对于电子邮件列表,您可以为您拥有的每封电子邮件创建/共享内容.
一些有用的链接:
也
你需要Google.Apis.Drive.v2 nuget包