gpt4 book ai didi

c# - 无法使用服务帐户在谷歌驱动器上上传图片

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:55 26 4
gpt4 key购买 nike

我正在尝试使用服务帐户将图像文件上传到谷歌驱动器。
在服务帐户中,我按照以下步骤操作:
1) 在开发者控制台创建新项目
2) 在 APIs & auth 中打开驱动器 api。
3) service account下生成新的client id
4) 同时生成P12 key 。
之后,我编写了以下代码,用于使用 SERVICE ACCOUNT 在 google drive 上上传图片:

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”为空。

谁能帮我解决这个问题?

最佳答案

您的代码与我通常使用的代码没有什么不同。我唯一能看到你可能缺少的是 parent 。我在文档中看不到任何提到 parent 是必需的,但它可能是。

 /// <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;
}

Google-dotnet-Samples 中提取的代码GitHub上的项目

关于c# - 无法使用服务帐户在谷歌驱动器上上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com