更新文件ID为与我在Gdrive中共享的文件 [英] Update file with file id that is shared with me in Gdrive ,

查看:102
本文介绍了更新文件ID为与我在Gdrive中共享的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C#应用

我需要使用文件ID更新在Gdrive中共享的文件.

I need to update a file that is shared in Gdrive using File ID.

代码:

public static Google.Apis.Drive.v2.Data.File UpdateFile(DriveService service, 
String fileId, String newTitle, String newDescription, String newMimeType, 
String newFilename, bool newRevision)
        {
        try
        {
            // First, retrieve the file from the Google Drive.
            Google.Apis.Drive.v2.Data.File file = service.Files.Get(fileId).Fetch();

            // Set the file's new metadata.
            file.Title = newTitle;
           // file.Description = newDescription;
            file.MimeType = newMimeType;

            // Get the file's new content and read it into a memory stream
            byte[] byteArray = System.IO.File.ReadAllBytes(newFilename);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

            // Call the Update API method passing in the updated information.
            FilesResource.UpdateMediaUpload request = service.Files.Update(file, fileId, stream, newMimeType);
            // Tell Google Drive if this is a new revision of the file or not.
            request.NewRevision = newRevision;
            // Execute the update
            request.Upload();

            // Get the response back from Google Drive and set the updatedFile 
            //object to the returned File informational object
            Google.Apis.Drive.v2.Data.File updatedFile = request.ResponseBody;
            // Return the updated file object so the caller has a handle on it.
            return updatedFile;
        }

引发错误:未找到文件.

推荐答案

两件事:

  1. 请将该库更新为最新版本.您可以从NuGet下载最新的驱动器API- http://www.nuget. org/packages/Google.Apis.Drive.v2/. 然后,您可以使用最新的Upload API,该API解决了一些错误. 还要注意,从1.4.0-beta开始,我们将Fetch方法更改为Execute,因此您将需要更改代码. 记住还要安装Google.Apis.Authentication软件包(在计划于几周后的下一个发行版中,我们将提供一个新的Auth库,该库将使OAuth2的舞蹈"变得更加轻松,并支持其他Windows平台,例如WinRT和WP).

  1. Please update the library to its latest version. You can download the latest drive API from NuGet - http://www.nuget.org/packages/Google.Apis.Drive.v2/. Then you can use the latest Upload API which solves several bugs. Notice also that from 1.4.0-beta we changed the Fetch method to Execute, so you will need to change your code. Remember to install also Google.Apis.Authentication package (in the next release which scheduled to be several weeks from now, we are going to present a new Auth library which will make the OAuth2 "dance" much easier and will support other Windows platforms like WinRT and WP as well).

您确定错误是从"request.Upload()"引发的,而不是从"byte [] byteArray = System.IO.File.ReadAllBytes(newFilename);"引发的.或"service.Files.Get(fileId).Fetch();".您可以附加确切的异常及其堆栈跟踪吗?

Are you sure that the error is thrown from "request.Upload()" and not from "byte[] byteArray = System.IO.File.ReadAllBytes(newFilename);" or "service.Files.Get(fileId).Fetch();". Can you attach the exact exception and its stacktrace?

Eyal

这篇关于更新文件ID为与我在Gdrive中共享的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆