【问题标题】:Google Drive API upload HTTP Multipart Unity3dGoogle Drive API上传HTTP Multipart Unity3d
【发布时间】:2021-07-06 04:13:35
【问题描述】:

我有这种代码

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 文件部分,则会出现格式错误的多部分正文,如果我我会丢失结束边界尝试将 jsonMetadata 与“Content-Type”、“multipart/related;boundary=foo_bar_baz”一起使用。

我该如何解决这个问题?

编辑。 我试图让我当前的 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--

我真的不明白我需要更改什么,因为我仍然收到“Missing end boundary in multipart body”。

--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;

【问题讨论】:

  • @DaImTo 我已经读过了。我不认为这是问题
  • 与 Unity3d formData 兼容。实际上,我在 Unity 网站的“类似 stackoverflow”的网站中找到了自己的部分答案。我会在 1 小时内(尽快)接受我自己的答案。请不要关闭这个问题,没有必要,它可以帮助很多需要在Unity3d中使用谷歌驱动API的人(谷歌没有官方支持,没有官方框架)。

标签: c# unity3d file-upload google-drive-api


【解决方案1】:

好的,我终于能够解决这个问题(我可以说我现在不确定它是如何工作的,但我可以肯定地说我需要使用 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 处理正文中的字节数组)就完成了。

【讨论】:

    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 2017-09-21
    相关资源
    最近更新 更多