asp.net – 动态创建和共享Google云端硬盘文件夹

前端之家收集整理的这篇文章主要介绍了asp.net – 动态创建和共享Google云端硬盘文件夹前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个电子邮件列表.

对于每封电子邮件,我需要创建一个Google云端硬盘文件夹并与给定的电子邮件共享.

我如何以编程方式进行操作?

我正在使用ASP.NET 4.0.

解决方法

首先,您需要确保您具有配置了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",});
}

步骤3 – 创建文件夹(或任何其他内容):

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");
}

对于电子邮件列表,您可以为您拥有的每封电子邮件创建/共享内容.

一些有用的链接

Creating files

Sharing files


你需要Google.Apis.Drive.v2 nuget包

猜你在找的asp.Net相关文章