我正在尝试使用 Google Drive Api 将图像上传到 Google 云端硬盘。我们的网站托管在 Azure 服务器上。上传可以在本地 PC 和非云端的其他物理服务器上进行。
insertRequest.ResponseBody 调用后为 null
insertRequest.Upload()
。
但我无法收到任何错误消息。这是我正在使用的代码;
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { DriveService.Scope.DriveFile }
}.FromCertificate(certificate));
// Create the service using the client credentials.
service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = applicationName
});
var uploadStream = new System.IO.MemoryStream(Convert.FromBase64String(base64));
// Get the media upload request object.
Google.Apis.Drive.v2.FilesResource.InsertMediaUpload insertRequest = service.Files.Insert(
new File
{
Title = fileTitle,
Parents = new List<ParentReference>() { new ParentReference() { Id = folderId } }
},
uploadStream,
"image/jpeg");
insertRequest.ProgressChanged += Upload_ProgressChanged;
insertRequest.ResponseReceived += Upload_ResponseReceived;
insertRequest.Upload();
File file = insertRequest.ResponseBody;
最佳答案
我们解决了这个问题。这完全是我们的错误。
不存在具有给定 ID 的文件夹。提供现有文件夹的 ID 解决了问题。
当不存在具有给定 ID 的文件夹时,它会抛出异常“对象引用未设置到对象的实例”。首先,我们认为这是我们的 C# 代码中的一个异常(exception)。但显然,Google SDK 抛出了它。
关于c# - 在 Azure 中,Google Drive Api 文件上传失败,返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36943955/