在谷歌驱动器帐户上传文件

时间:2014-07-17 08:56:03

标签: 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();
    }

0 个答案:

没有答案