【发布时间】:2019-12-04 23:49:49
【问题描述】:
我正在尝试将文件上传到我自己的谷歌驱动器。 我所做的是 1.在谷歌驱动器上创建项目 2.为服务帐户创建凭据 3.为一个用户创建安全json,并赋予用户作为所有者的权限 现在使用服务为用户提供的电子邮件和下载的 json 文件,尝试上传文件/创建文件夹。 没有错误,我正在取回文件 ID。
但是,当我在我的驱动器上检查它时,我没有看到上传的文件。 这是我的代码:
using Google.Apis.Auth.OAuth2;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Google.Apis.Auth;
public void GetService()
{
string filename = "dheerajsecudata.json";
string email = "dheeraj-588@secudata.iam.gserviceaccount.com";
var service = AuthenticateServiceAccount(email, filename);
CreateFolder("BlahBlah", service);
var fileMetadata = new Google.Apis.Drive.v2.Data.File()
{
Title = "BrainData/bpl1.jpg"
};
FilesResource.InsertMediaUpload request;
using (var stream = new System.IO.FileStream(@"D:/bpl1.jpg",
System.IO.FileMode.Open))
{
request = service.Files.Insert(
fileMetadata, stream, "image/jpeg");
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);//Getting ID but no folder / files on drive
}
private static void CreateFolder(string folderName, DriveService service)
{
var fileMetadata = new Google.Apis.Drive.v2.Data.File()
{
Title = folderName,
MimeType = "application/vnd.google-apps.folder"
};
var request = service.Files.Insert(fileMetadata);
request.Fields = "id";
var file = request.Execute();
Console.WriteLine("Folder ID: " + file.Id);
}
public static DriveService AuthenticateServiceAccount(string serviceAccountEmail, string serviceAccountCredentialFilePath)
{
try
{
if (string.IsNullOrEmpty(serviceAccountCredentialFilePath))
throw new Exception("Path to the service account credentials file is required.");
if (!System.IO.File.Exists(serviceAccountCredentialFilePath))
throw new Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath);
if (string.IsNullOrEmpty(serviceAccountEmail))
throw new Exception("ServiceAccountEmail is required.");
// These are the scopes of permissions you need. It is best to request only what you need and not all of them
//string[] scopes = new string[] { AnalyticsReportingService.Scope.Analytics }; // View your Google Analytics data
string[] scopes = { DriveService.Scope.Drive, DriveService.Scope.DriveFile };
// For Json file
if (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() == ".json")
{
GoogleCredential credential;
using (var stream = new FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(scopes);
}
// Create the Analytics service.
return new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive Service account Authentication Sample",
});
}
else
{
throw new Exception("Unsupported Service accounts credentials.");
}
}
catch (Exception ex)
{
Console.WriteLine("Create service account DriveService failed" + ex.Message);
throw new Exception("CreateServiceAccountDriveFailed", ex);
}
}
【问题讨论】:
-
我已经尝试过使用 .p12 文件。没运气。我正在返回 fileid 但驱动器上没有文件。
标签: c# google-drive-api