上传文件谷歌驱动器应用程序

时间:2014-07-16 11:53:39

标签: c# file-upload oauth-2.0 google-drive-api

我希望任何用户输入他的密码和电子邮件,并在程序中选择文件(访问BD)。然后按一个按钮将文件上传到谷歌硬盘上的帐户。

这是我目前的代码,这会在我的计数中创建一个目录:

    UserCredential Credential;

        Credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets { ClientId = "client_id", ClientSecret = "client_secret" },
            new[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile },
            "user",
            CancellationToken.None,
            new FileDataStore("Drive.Auth.Store")).Result;

        DriveService service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = Credential,
            ApplicationName = "Drive API hoteltactil",
        });

        Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
        body.Title = "NewDirectory2";
        body.Description = "Test Directory";
        body.MimeType = "application/vnd.google-apps.folder";
        body.Parents = new List<ParentReference>() { new ParentReference() { Id = "root" } };
        try
        {
            FilesResource.InsertRequest request = service.Files.Insert(body);
            request.Execute();
        }
        catch (Exception ex)
        {
            Console.WriteLine("An error occurred: " + ex.Message);
            Console.Read();
        }

2 个答案:

答案 0 :(得分:0)

我不太确定你在问什么,但是为了改变我已经完成的文件的所有权:

 main
 {  
 Permission permission= new Permission();
 permission = share(service, file.Id, <user email>, "user", "owner");
 }

 public static Permission share(DriveService service, String fileId, String value,
  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;
    }

另外,如果要上传到根文件夹,则不需要指定body.Parents,因为root是基本文件夹(即自动父文件夹)

答案 1 :(得分:0)

我认为您不需要允许用户登录您的帐户,

您可以创建一个链接URL,然后与感兴趣的用户共享此链接,

那样,他们只会看到相关文件,也可以下载...