gpt4 book ai didi

C# Google Api 上传流

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

我已经阅读了一些问题并尝试了这些示例并且它们对我有用,这里是链接:- Link 1- Link 2- Link 3

但是,我试图完成的有点不同,我正在尝试修改/上传一个包含纯文本的 .DAT 文件。我能够毫无问题地读取文件内容,但我总是收到 400 Bad Request(协议(protocol)错误)。这是我的代码。

   DocumentsService service = new DocumentsService("Man");
service.setUserCredentials(UserName, Passwod);

DocumentsListQuery fileQuery = new DocumentsListQuery();
fileQuery.Query = User.FileName;
fileQuery.TitleExact = true;
DocumentsFeed feed = service.Query(fileQuery);

//Here I get my file to update
DocumentEntry entry = (DocumentEntry)feed.Entries[0];

// Set the media source
//Here I have tried application/octet-stream also
entry.MediaSource = new MediaFileSource(DataStream, User.FileName, text/plain");

// Instantiate the ResumableUploader component.
ResumableUploader uploader = new ResumableUploader();

// Set the handlers for the completion and progress events
uploader.AsyncOperationCompleted += new AsyncOperationCompletedEventHandler(OnDone);
uploader.AsyncOperationProgress += new syncOperationProgressEventHandler(OnProgress);

ClientLoginAuthenticator authenticator = new ClientLoginAuthenticator("Man", ServiceNames.Documents,Credentials);
Uri updateUploadUrl = new Uri(UploadUrl);
AtomLink aLink = new AtomLink(updateUploadUrl.AbsoluteUri);
aLink.Rel = ResumableUploader.CreateMediaRelation;
entry.Links.Add(aLink);

// Start the update process.
uploader.UpdateAsync(authenticator,entry,new object());

谢谢。

编辑:这就是我解决它的方法。感谢 Claudio 指导我正确的方向


  1. 从以下位置下载示例应用程序:Download Sample (.zip)
  2. 从 SampleHelper 项目为您的项目实现这些:AuthorizationMgr、INativeAuthorizationFlow、LoopbackServerAuthorizationFlow、WindowTitleNativeAuthorizationFlow
  3. 将其与此代码一起使用:

    //Call Authorization method... (omitted)

    File body = new File();
    body.Title = title;
    body.Description = description;
    body.MimeType = mimeType;
    byte[] byteArray = System.IO.File.ReadAllBytes(filename);
    MemoryStream stream = new MemoryStream(byteArray);

    try {
    FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);
    request.Upload();

    File file = request.ResponseBody;

    // Uncomment the following line to print the File ID.
    // Console.WriteLine("File ID: " + file.Id);

    return file;
    } catch (Exception e) {
    Console.WriteLine("An error occurred: " + e.Message);
    return null;
    }

最佳答案

每当服务器返回 400 Bad Request 时,它还会包含一条描述性错误消息,告诉您请求中的无效内容。如果无法从调试器获取它,则应安装 Fiddler 并捕获 HTTP 请求和响应。

我给您的另一个建议是开始使用较新的 Drive API 而不是 Documents List API。这是一个 5 分钟的 C# 快速入门示例,展示了如何将文件上传到 Google 云端硬盘:

https://developers.google.com/drive/quickstart

关于C# Google Api 上传流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12222113/

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