How to upload/download a file with Microsoft graph

2019-07-28 21:34发布

问题:

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

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.

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

and I get this error:

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"
  }
}

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

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

or maybe in the permission.

I don't really know.

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.

回答1:

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.

Something more like this:

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