【问题标题】:C# Google Api Upload StreamC# Google Api 上传流
【发布时间】:2012-08-31 20:31:07
【问题描述】:

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

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

   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());

谢谢。

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


  1. 从以下位置下载示例应用程序:Download Sample (.zip)
  2. 从 SampleHelper Project 为您的项目实施这些: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;
    }
    

【问题讨论】:

    标签: c# google-drive-api google-docs-api


    【解决方案1】:

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

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

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

    【讨论】:

    • 该库包含一个用于 Tasks API (code.google.com/p/google-api-dotnet-client/source/…) 的 Winforms 示例,将它与我发送给您的 Drive 示例混合起来应该不难
    • 嗨...看...我已经检查了您给我的链接...但是没有 Google Drive 的示例...访问此链接:code.google.com/p/google-api-dotnet-client/wiki/APIs#Drive_API
    • 感谢您的帮助,我得到了最后一个问题...每次我运行我的使用云端硬盘上传文件的示例时,它都会要求我允许访问,然后再给我 AuthorizationCode...
    • 这是预期的行为,除非您将刷新令牌保存在数据库中以便将来运行时重复使用。
    猜你喜欢
    • 1970-01-01
    • 2020-03-10
    • 2020-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多