2

I have read a few questions around and i tried those examples and they worked for me, here are links: - Link 1 - Link 2 - Link 3

However, What i'm tryin to accomplish its a little bit different, im trying to modify/upload a .DAT file that contains plain text inside. Im able to read file contents with no problems but i always get 400 Bad Request(Protocol Error). Heres my code.

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

Thanks.

EDIT: This is how i solved it. Thanks to Claudio for guide me on the proper direction


  1. Download Example Application from : Download Sample (.zip)
  2. Implement to your project from SampleHelper Project these: AuthorizationMgr, INativeAuthorizationFlow, LoopbackServerAuthorizationFlow, WindowTitleNativeAuthorizationFlow
  3. Use it with this code:

    //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;
    }
    
Community
  • 1
  • 1
w0rtez
  • 21
  • 3

1 Answers1

2

Whenever the server returns a 400 Bad Request, it also includes a descriptive error message telling what is invalid in your request. If you can't get to it from the debugger, you should install Fiddler and capture the HTTP request and response.

Another advice I have for you is to start using the newer Drive API instead of the Documents List API. Here is a 5-minute C# quickstart sample showing how to upload a file to Google Drive:

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

Claudio Cherubino
  • 14,896
  • 1
  • 35
  • 42
  • The library includes a Winforms sample for the Tasks API (http://code.google.com/p/google-api-dotnet-client/source/browse?repo=samples#hg%2FTasks.WinForms.NoteMgr), it shouldn't be hard to mix that with the Drive sample I sent you – Claudio Cherubino Sep 01 '12 at 18:30
  • Hi... look.. i have check the link you gave me... but theres no examples for the Google Drive... Visit this link: http://code.google.com/p/google-api-dotnet-client/wiki/APIs#Drive_API – w0rtez Sep 03 '12 at 14:15
  • Thanks for your help, i got 1 last question... Everytime i run my example for uploading file with Drive, it asks me for Allow Access, before giving me the AuthorizationCode... – w0rtez Sep 04 '12 at 20:14
  • That's the expected behavior unless you save the refresh token in a database to reuse it for future runs. – Claudio Cherubino Sep 05 '12 at 08:05