如何使用Rest api和oauth2.0创建文件夹和上传文件

时间:2013-05-06 13:35:49

标签: c# java rest oauth-2.0 onedrive

我想使用rest api创建一个文件夹并上传文件  我的代码是这样的

  public string CreateFolder(string FolderName)
    {
        int WorkSpaceId = 330201;
        int id = 168079105;
        var queryString = HttpContext.Current.Session["tokenSession"];
        var request = WebRequest.Create(RequestProfileUrl + FolderName);
        request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.None;
        request.Headers.Add("Authorization", "Bearer " + AccessToken);
        request.ContentType = "multipart/form-data";
        request.Method = "POST";
        var response = request.GetResponse();
        HttpContext.Current.Response.Redirect("" + request.RequestUri);
        using (var responseStream = response.GetResponseStream())
        {
            var reader = new StreamReader(responseStream);
            var responseText = reader.ReadToEnd();
            reader.Close();
            return responseText;
        }
    }

我必须这样做

POST https://apis.live.net/v5.0/me/skydrive
Authorization: Bearer ACCESS_TOKEN
Content-Type: multipart/form-data

{
    "name": "My example folder"
}`

我添加了请求标头和内容类型,我不知道如何在我的请求中添加name参数。

3 个答案:

答案 0 :(得分:0)

我认为应该进入你的请求机构。您可能需要调用writeMessageBody并根据需要编写正文。

答案 1 :(得分:0)

您可以参考以下链接

SlideShare Click Here dropbox Click Here

最后但并非最不重要的MSDN:

MSDN Click Here

答案 2 :(得分:0)

要编写POST请求的正文,您需要先获取请求流,然后写入它。请参阅下面的示例代码。请注意,我将Content-Type从“multipart / form-data”更改为“application / json”,因为这就是您的数据。

       // String with the body content 
        string postBody = "{\"name\":\"myfoldername\"}";
        ASCIIEncoding encoding = new ASCIIEncoding ();
        byte[] byte1 = encoding.GetBytes (postBody);

        // Set Content type to application/json
        myHttpWebRequest.ContentType = "application/json";

        // Set content length of the string being posted.
        myHttpWebRequest.ContentLength = byte1.Length;

        // Get the request stream and write body bytes to it
        Stream newStream = myHttpWebRequest.GetRequestStream ();
        newStream.Write (byte1, 0, byte1.Length);