我有这种代码
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("metadata","{ \"name\":" + salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json" + ", \"parents\":['" + entries.Files[0].Id + "'] }","application/json"));
formData.Add(new MultipartFormDataSection("file", Application.persistentDataPath + "/Saves/" + salvataggio2, "application/json"));
//formData.Add(new MultipartFormFileSection(salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json", Application.persistentDataPath + "/Saves/" + salvataggio2));
//string jsonMetadata = "--foo_bar_baz" + System.Environment.NewLine + " Content-Type: application/json; charset=UTF-8 { \"name\":" + salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json" + ", \"parents\":['" + entries.Files[0].Id + "'] }" + System.Environment.NewLine + "--foo_bar_baz" + System.Environment.NewLine + "Content-Type: application/json; charset=UTF-8" + File.ReadAllText(Application.persistentDataPath + "/Saves/" + salvataggio2) + System.Environment.NewLine + "--foo_bar_baz--" + System.Environment.NewLine;
//byte[] bytes = Encoding.UTF8.GetBytes(jsonMetadata);
using (UnityWebRequest www = UnityWebRequest.Post("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", formData)) {
//var upload = new UploadHandlerRaw(bytes);
//www.uploadHandler = upload;
www.SetRequestHeader("Authorization", "Bearer " + response.Access_token);
//www.SetRequestHeader("Content-Type", "multipart/related; boundary=foo_bar_baz");
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError) {
Debug.Log(www.downloadHandler.text);
this.gameObject.SetActive(false);
} else {
Debug.Log(www.downloadHandler.text);
}
}
如你所见,我需要上传一个json文件到google Drive。我已经获得父文件夹的文件ID。不知道我做错了什么,但我有多个错误。就像
“code”:400,“Message”:“Parse Error”(用于当前未注释的代码),如果我尝试使用MultipartFormFileSection部分而不是MultipartFormDataSection文件部分,则MultiPartBody格式不正确,如果我尝试使用具有“Content-Type”的jsonMetadata,“multipart/Related;boundary=foo_bar_baz”,则缺少结束边界。
我该如何解决这个问题呢?
编辑。我尝试对当前的jsonMetadata变量(包含所有内容的变量)进行Log操作,更正了一些内容,得到了以下结果
--foo_bar_baz
Content-Disposition: form-data; name="metadata"
Content-Type: application/json; charset=UTF-8
{ "name":"Autosave - Lief-to-google-drive.json", "parents":["1N7pYSBW-eI-sMcS0KF4cjd9IuTYVTani"] }
--foo_bar_baz
Content-Disposition: form-data; name="file"
Content-Type: application/json; charset=UTF-8
{"nickname":"Lief","sex":false,"startingPoint":[0.0,0.0,-5.0],"startingRotation":[0.0,0.0,0.0],"startingCameraPoint":[0.0,0.0,0.0],"startingCameraRotation":[0.0,0.0,0.0],"npcRep":[{"dialogNumber":"00001","rep":0,"side":"default"}],"sideRep":[{"side":"default","rep":0},{"side":"demo","rep":-100}]}
--foo_bar_baz--
我并不是真的没有意识到我需要改变什么,因为我仍然得到“多部分体中缺少末端边界”。
--foo_bar_baz--应该是结尾
我的代码部分现在像这样
string jsonMetadata = "--foo_bar_baz" + System.Environment.NewLine
+ "Content-Disposition: form-data; name=\"metadata\"" + System.Environment.NewLine
+ "Content-Type: application/json; charset=UTF-8" + System.Environment.NewLine + System.Environment.NewLine
+ "{ \"name\":\"" + salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json\"" + ", \"parents\":[\"" + entries.Files[0].Id + "\"] }" + System.Environment.NewLine
+ "--foo_bar_baz" + System.Environment.NewLine
+ "Content-Disposition: form-data; name=\"file\"" + System.Environment.NewLine
+ "Content-Type: application/json; charset=UTF-8" + System.Environment.NewLine + System.Environment.NewLine
+ File.ReadAllText(Application.persistentDataPath + "/Saves/" + salvataggio2) + System.Environment.NewLine
+ "--foo_bar_baz--" + System.Environment.NewLine;
好的,我终于解决了这个问题(我可以说的是,我现在不确定它是如何工作的,但我可以肯定地说,我需要用Unity api创建边界)
List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
formData.Add(new MultipartFormDataSection("metadata", "{ \"name\":\"" + salvataggio2.Substring(0, salvataggio2.Length - 5) + "-to-google-drive.json\"" + ", \"parents\":[\"" + entries.Files[0].Id + "\"] }", "application/json"));
formData.Add(new MultipartFormDataSection("file", File.ReadAllText(Application.persistentDataPath + "/Saves/" + salvataggio2), "application/json"));
byte[] boundary = UnityWebRequest.GenerateBoundary();
byte[] formSections = UnityWebRequest.SerializeFormSections(formData, boundary);
byte[] terminate = Encoding.UTF8.GetBytes(String.Concat("\r\n--", Encoding.UTF8.GetString(boundary), "--"));
byte[] body = new byte[formSections.Length + terminate.Length];
Buffer.BlockCopy(formSections, 0, body, 0, formSections.Length);
Buffer.BlockCopy(terminate, 0, body, formSections.Length, terminate.Length);
string contentType = String.Concat("multipart/related; boundary=", Encoding.UTF8.GetString(boundary));
using (UnityWebRequest www = UnityWebRequest.Post("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart", "")) {
var upload = new UploadHandlerRaw(body);
www.uploadHandler = upload;
www.SetRequestHeader("Authorization", "Bearer " + response.Access_token);
www.SetRequestHeader("Content-Type", contentType);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError || www.result == UnityWebRequest.Result.ProtocolError) {
Debug.Log(www.downloadHandler.text);
this.gameObject.SetActive(false);
} else {
Debug.Log(www.downloadHandler.text);
}
}
正如您所看到的,在前2个添加到formData之后(与前面相同),它创建了一个随机边界,它序列化了表单和边界,它创建了一个终止,它融合了所有内容,并且它创建了一个兼容的内容类型。在此之后,我只需创建通常的Unity Post(在正文中为字节数组使用UploadHandler)就完成了。