0

A problem has occurred recently when trying to upload files with the Google API dotnet client sdk. When the file name has any special Unicode characters, it throws an error.

Here is my code

    public static Google.Apis.Drive.v2.Data.File InsertResource(Google.Apis.Drive.v2.DriveService service, string filePath, string parentId, string fileName, string mimeType)
    {
        Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();

        try
        {
            // File's metadata.

            body.Title = fileName;
            body.MimeType = mimeType;

            // Set the parent folder.
            if (!String.IsNullOrEmpty(parentId))
            {
                var response = DriveUtils.GetFileInfo(service, parentId);
                if (response.Error != null)
                {
                    body.Error = response.Error;
                    return body;
                }
                body.Parents = new List<ParentReference>() { new ParentReference() { Id = parentId } };
            }

            byte[] byteArray = System.IO.File.ReadAllBytes(filePath);
            MemoryStream stream = new MemoryStream(byteArray);

            Google.Apis.Drive.v2.FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);

            request.Upload();
            return request.ResponseBody;

        }
        catch (GoogleApiRequestException e)
        {
            body.Error = e.RequestError;
            return body;
        }
}

It was working fine up until this week. Any files that have chinese characters or turkish characters in the name will throw an error.

at Google.Apis.Json.JsonReader.ParseExpression(JsonToken token, TokenStream ts) at Google.Apis.Json.JsonReader.Parse(String jsonAsText) at Google.Apis.Upload.ResumableUpload`1.Upload()

  • I'm getting the same problem, but I'm using webRequest directly, so I only get the error when the json object hits the google drive server: http://stackoverflow.com/questions/17431572/cant-upload-filename-with-special-character-to-google-drive-rest – IPValverde Jul 03 '13 at 12:11

1 Answers1

2

Try to download the latest version of the API (from https://code.google.com/p/google-api-dotnet-client/wiki/APIs#Drive_API). I changed line 122 in the Drive sample (instructions to download this sample are here), to Title = "字/漢字" and Title = "title with ç", and both of them worked for me.

peleyal
  • 3,472
  • 1
  • 14
  • 25