c# - 在 Azure 中,Google Drive Api 文件上传失败,返回 null

标签 c# azure google-drive-api

我正在尝试使用 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/

相关文章:

c# - ASP.NET Core websocket 管理器 - websockets 总是被释放

azure - 将 SSL 证书分配给 Azure 应用服务

c# - 所有任务完成后我的用户界面就会更新

c# - 如何从同一解决方案检查另一个项目的版本

Azure Functions - 缩放和内存限制

azure - (Azure APGW/SSL 证书)Azure 应用程序网关中缺少中间证书。证书错误?

android - 为 Google 云端硬盘文件设置多个父项

android - Google Drive API 迁移到 REST API

ios - iOS Google Drive SDK-文件夹的查询字符串似乎不起作用

c# - 将 Windows Phone 8 应用程序连接到远程 mysql 数据库