[英]Google Drive API upload HTTP Multipart Unity3d
我有这种代码
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 文件上传到谷歌驱动器。 我已经得到了父文件夹的fileId。 不知道我做错了什么,但我遇到了多个错误。 喜欢
“code”:400,“message”:“Parse Error”(对于现在未注释的代码),如果我尝试使用 MultipartFormFileSection 部分而不是 MultipartFormDataSection 文件部分,则多部分正文格式错误,如果我尝试使用,则缺少结束边界具有“Content-Type”、“multipart/related;boundary=foo_bar_baz”的 jsonMetadata。
我怎么解决这个问题?
编辑。 我试图让我当前的 jsonMetadata 变量的日志(包含所有内容的变量)纠正了一些事情,我得到了这个
--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 用于正文中的字节数组),它就完成了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.