Hey everyone,
I’m having trouble with a project where I need to upload different file types to Google Docs using C#. So far, I’ve managed to get text and Word files working fine. But when I try to upload PDF or Excel files, I keep getting a bad request error. It’s driving me crazy!
I’m using Single Sign-On (SSO) for Google login with ASP.NET C#. Has anyone run into this issue before? Any tips or tricks for handling these file types specifically?
I’d really appreciate any help or advice you can give. Thanks a bunch in advance!
// Example code snippet
public async Task<bool> UploadFile(string filePath, string fileType)
{
try
{
var service = new DocsService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "MyApp"
});
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = Path.GetFileName(filePath),
MimeType = GetMimeType(fileType)
};
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open))
{
request = service.Files.Create(fileMetadata, stream, GetMimeType(fileType));
request.Fields = "id";
await request.UploadAsync();
}
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Error uploading file: {ex.Message}");
return false;
}
}