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
- Download Example Application from : Download Sample (.zip)
- Implement to your project from SampleHelper Project these: AuthorizationMgr, INativeAuthorizationFlow, LoopbackServerAuthorizationFlow, WindowTitleNativeAuthorizationFlow
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; }