C# 无法使用服务帐户在google drive上上载图像
我正在尝试使用服务帐户将图像文件上载到google drive。C# 无法使用服务帐户在google drive上上载图像,c#,.net,google-drive-api,google-api-dotnet-client,C#,.net,Google Drive Api,Google Api Dotnet Client,我正在尝试使用服务帐户将图像文件上载到google drive。 在服务帐户中,我遵循以下步骤: 1) 在开发人员控制台上创建了新项目 2) 在api&auth中打开驱动器api。 3) 在服务帐户下生成的新客户端id 4) 还生成了P12密钥。 之后,我编写了以下代码,用于使用服务帐户在谷歌硬盘上上传图像: X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasec
在服务帐户中,我遵循以下步骤:
1) 在开发人员控制台上创建了新项目
2) 在api&auth中打开驱动器api。
3) 在服务帐户下生成的新客户端id
4) 还生成了P12密钥。
之后,我编写了以下代码,用于使用服务帐户在谷歌硬盘上上传图像:
X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL)
{
Scopes = new[] { DriveService.Scope.DriveReadonly }
}.FromCertificate(certificate));
// Create the service.
var service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",
});
File body = new File();
body.Title = "My Sample Image";
body.Description = "Image";
body.MimeType = "image/jpeg";
byte[] byteArray = System.IO.File.ReadAllBytes("image.jpeg");
System.IO.File.Delete("image.jpeg");
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "image/jpeg");
request.Upload();
File file = request.ResponseBody;
上传失败,当我尝试调试代码时,我发现“request.ResponseBody”为空
有人能帮我一下吗?你的代码和我通常使用的代码没什么不同。我能马上看到的唯一一件事就是你的父母。我看不到文件中有任何提到家长是必需的,但可能是
/// <summary>
/// Uploads a file
/// Documentation: https://developers.google.com/drive/v2/reference/files/insert
/// </summary>
/// <param name="_service">a Valid authenticated DriveService</param>
/// <param name="_uploadFile">path to the file to upload</param>
/// <param name="_parent">Collection of parent folders which contain this file.
/// Setting this field will put the file in all of the provided folders. root folder.</param>
/// <returns>If upload succeeded returns the File resource of the uploaded file
/// If the upload fails returns null</returns>
public static File uploadFile(DriveService _service, string _uploadFile, string _parent) {
if (System.IO.File.Exists(_uploadFile))
{
File body = new File();
body.Title = System.IO.Path.GetFileName(_uploadFile);
body.Description = "File uploaded by Diamto Drive Sample";
body.MimeType = GetMimeType(_uploadFile);
body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };
// File's content.
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
request.Upload();
return request.ResponseBody;
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
return null;
}
}
else {
Console.WriteLine("File does not exist: " + _uploadFile);
return null;
}
}
private static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
//
///上载文件
///文件:https://developers.google.com/drive/v2/reference/files/insert
///
///有效的经过身份验证的驱动器服务
///要上载的文件的路径
///包含此文件的父文件夹的集合。
///设置此字段将把文件放在所有提供的文件夹中。根文件夹。
///如果上载成功,则返回上载文件的文件资源
///如果上载失败,则返回null
公共静态文件上传文件(DriveService\u服务、字符串\u上传文件、字符串\u父级){
if(System.IO.File.Exists(_uploadFile))
{
文件体=新文件();
body.Title=System.IO.Path.GetFileName(_uploadFile);
body.Description=“Diamto驱动样本上传的文件”;
body.MimeType=GetMimeType(_uploadFile);
body.Parents=new List(){new ParentReference(){Id=\u parent}};
//文件的内容。
byte[]byteArray=System.IO.File.ReadAllBytes(\u uploadFile);
System.IO.MemoryStream stream=新的System.IO.MemoryStream(byteArray);
尝试
{
fileResource.InsertMediaUpload请求=_service.Files.Insert(body、stream、GetMimeType(_uploadFile));
request.Upload();
返回request.ResponseBody;
}
捕获(例外e)
{
Console.WriteLine(“发生错误:+e.Message”);
返回null;
}
}
否则{
WriteLine(“文件不存在:”+\u uploadFile);
返回null;
}
}
私有静态字符串GetMimeType(字符串文件名)
{
字符串mimeType=“应用程序/未知”;
字符串ext=System.IO.Path.GetExtension(文件名).ToLower();
Microsoft.Win32.RegistryKey regKey=Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if(regKey!=null&®Key.GetValue(“内容类型”)!=null)
mimeType=regKey.GetValue(“内容类型”).ToString();
返回mimeType;
}
从GitHub上的项目中撕下的代码我添加了父项并将其设置为“根”,但它对我不起作用..我得到以下异常:异常={“值不能为空。\r\n参数名称:baseUri”}它是否返回任何错误?您是否尝试过使用listfiles查看文件是否正在上载?我只是更新了示例项目以使用服务帐户或Oauth2。演示如何上载、更新和下载文件。@DaImTo哎呀,我的坏消息。我的意思是问操作系统,您的代码在哪里显示“File File=request.ResponseBody;”,文件包含什么?