Google 驱动 API 上传 HTTP Multipart Unity3d

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 文件上传到 google 驱动器。我已经得到了父文件夹的fileId。 不确定我做错了什么,但我遇到了多个错误。喜欢

"code": 400, "message": "Parse Error" (for the code not commented right now), malformed multipart body if I try to use the MultipartFormFileSection part instead of the MultipartFormDataSection file part, missing end boundaries if I try to use the jsonMetadata with the "Content-Type", "multipart/related; boundary=foo_bar_baz".

我该如何解决这个问题?

编辑。 我试图让我当前的 json 元数据变量(包含所有内容的那个)的日志更正一些东西 我得到了这个

--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 处理正文中的字节数组)就完成了。