2014-07-17 136 views
0

無法通過驅動器連接API與Google雲端硬盤我的Windows窗體應用程序。谷歌驅動器和.NET的WinForms

我已經創建了https://console.developers.google.com/項目,使驅動器API,生成的客戶端ID,服務的電子郵件和*或.p12關鍵。

我用這種方法實例DriveService:

private const string SERVICE_ACCOUNT_EMAIL = "[email protected]"; 
private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"PATH_.p12"; 

static DriveService BuildService() 
{ 
    X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable); 

    ServiceAccountCredential credential = new ServiceAccountCredential(
    new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL) 
    { 
     User = "O[email protected]", 
     Scopes = new[] { DriveService.Scope.DriveFile } 
    }.FromCertificate(certificate)); 

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

    return service; 
} 

,並嘗試通過這個方法來上傳文件:

public static void upload(String filepath) 
{ 
    File body = new File(); 
    body.Title = "test"; 
    body.Description = "Some description"; 
    body.MimeType = "text/plain"; 

    byte[] byteArray = System.IO.File.ReadAllBytes(filepath); 
    System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray); 

    FilesResource.InsertMediaUpload request = BuildService().Files.Insert(body, stream, "text/plain"); 
    request.Upload(); 

    File file = request.ResponseBody; 
} 

然而,request.ResponceBody始終爲空。我錯過了什麼?

+1

你可能想從這些代碼中刪除個人信息...... – GSerg

+0

我不認爲會有人能夠利用它,但無論如何,我是。 – koryakinp

+0

你應該嘗試上傳 –

回答

1

我找到了解決辦法:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Security.Cryptography.X509Certificates; 
using Google.Apis.Auth.OAuth2; 
using Google.Apis.Drive.v2; 
using Google.Apis.Drive.v2.Data; 
using Google.Apis.Services; 
using System.IO; 
using System.Web; 

//... 

    public static DriveService BuildService() 
    { 
     string SERVICE_ACCOUNT_EMAIL = "[email protected]"; 
     string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"\YYYYYYY.p12"; 

     return new DriveService(new BaseClientService.Initializer() 
     { 
      HttpClientInitializer = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL) 
      { 
       Scopes = new[] { DriveService.Scope.Drive } 
      }.FromCertificate(new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable))), 
      ApplicationName = "applicationName" 
     }); 
    } 

    public static void insertFile(String filePath) 
    { 
     Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File(); 
     body.Title = Path.GetFileNameWithoutExtension(filePath); 
     body.MimeType = MimeMapping.GetMimeMapping(filePath); 

     MemoryStream stream = new MemoryStream(System.IO.File.ReadAllBytes(filePath)); 
     FilesResource.InsertMediaUpload request = BuildService().Files.Insert(body, stream, body.MimeType); 
     request.Upload(); 
    }