如何在Unity3d中使用Microsoft graph API上传/下载文件 [英] How to upload/download a file with Microsoft graph API in Unity3d

查看:107
本文介绍了如何在Unity3d中使用Microsoft graph API上传/下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用Microsoft图形上的Unity3d进行oauth 2登录,我为我的应用请求了此权限: https://graph.microsoft.com/files.readwrite.appfolder

I was able to make a oauth 2 login with Unity3d on Microsoft graph, I requested this permission for my app: https://graph.microsoft.com/files.readwrite.appfolder

经过通常的代码流程(重定向到url,用户许可,将身份验证代码交换为令牌代码,并将令牌交换为承载身份验证代码),我得以登录.

After the usual code flow (redirect to url, permission from user, auth code exchanged for token code and token for bearer auth code) I was able to log in.

问题是无法上传小文件: https://docs.microsoft.com/zh-我们/onedrive/developer/rest-api/api/driveitem_put_content

Problem is that the upload of small files does not work: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_put_content

我认为这是我能做的最好的事情

I think this is the best I can do:

string myData = File.ReadAllText(Application.persistentDataPath + "/" + "provaupload.json");
    using (UnityWebRequest www = UnityWebRequest.Post("https://graph.microsoft.com/v1.0/me/drive/root:/AppTry/provaupload.json:/createUploadSession", myData)) {
        www.SetRequestHeader("Authorization", "Bearer <code>");
        www.SetRequestHeader("Content-Type", "application/json");
        yield return www.SendWebRequest();

        if (www.isNetworkError || www.isHttpError) {
            Debug.Log(www.error + " " + www.downloadHandler.text);
        } else {
            Debug.Log("Upload complete! " + www.downloadHandler.text);
        }
    }

我得到这个错误:

Generic/unknown HTTP error {
  "error": {
  "code": "BadRequest",
  "message": "Unable to read JSON request payload. Please ensure Content-Type header is set and payload is of valid JSON format.",
  "innerError": {
    "request-id": "id",
    "date": "2018-07-20T06:24:30"
  }
}

我也尝试了WWW类或Put而不是Post,但得到了无效的API". 也许我的问题出在基本网址中: https://graph.microsoft.com/v1.0/me

I also tried the WWW class or Put instead of Post but I get "invalid API". Maybe my problem is in the base url: https://graph.microsoft.com/v1.0/me

或者可能在路上 根目录:/AppTry/provaupload.json

or maybe it's in the path root:/AppTry/provaupload.json

或者可能在许可范围内.

or maybe in the permission.

我真的不知道.

如果您知道如何使用Microsoft Graph和一个驱动器进行Rest调用(即使不是在unity3d中,即使您不知道如何解决我的特定问题),也很高兴获得一些示例.

If you know how to make a Rest call with Microsoft Graph and One drive (even not in unity3d and even if you don't know how to solve my specific problem) it would be great to get some example.

推荐答案

要上传文件,请使用

To upload file, use the UploadHandler. You must also encode the string as UTF8. As you mentioned in the comment section, it looks like you have to use PUT instead of POST and the url should be changed to something else.

更像是这样:

string myData = File.ReadAllText(Application.persistentDataPath + "/" + "provaupload.json");
string url = "https://graph.microsoft.com/v1.0/me/drive/root:/AppTry/provaupload.json:/content";

using (UnityWebRequest www = new UnityWebRequest(url, "PUT"))
{
    byte[] dataToSend = new System.Text.UTF8Encoding().GetBytes(myData);
    www.uploadHandler = (UploadHandler)new UploadHandlerRaw(dataToSend);
    www.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();

    www.SetRequestHeader("Authorization", "Bearer <code>");
    www.SetRequestHeader("Content-Type", "application/json");
    yield return www.SendWebRequest();

    if (www.isNetworkError || www.isHttpError)
    {
        Debug.Log(www.error + " " + www.downloadHandler.text);
    }
    else
    {
        Debug.Log("Upload complete! " + www.downloadHandler.text);
    }
}

这篇关于如何在Unity3d中使用Microsoft graph API上传/下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆