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始終爲空。我錯過了什麼?
你可能想從這些代碼中刪除個人信息...... – GSerg
我不認爲會有人能夠利用它,但無論如何,我是。 – koryakinp
你應該嘗試上傳 –