使用REST上传SkyDrive API文件

时间:2012-05-01 12:00:57

标签: .net api rest onedrive

我正在使用以下代码测试SkyDrive的API:

Dim webRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://apis.live.net/v5.0/me/skydrive/files?access_token=" & Me.ACCESS_TOKEN), HttpWebRequest)
    webRequest.Method = "POST"
    webRequest.ContentType = "multipart/form-data; boundary=A300x"
    webRequest.KeepAlive = True
    webRequest.Timeout = 80000
    Using streamWriter As New System.IO.StreamWriter(webRequest.GetRequestStream())
      streamWriter.Write("--A300x\r\n")
      streamWriter.Write("Content-Disposition: form-data; name=""file""; filename=""HelloWorld.txt""\r\n")
      streamWriter.Write("Content-Type: application/octet-stream\r\n")
      streamWriter.Write("\r\n")
      streamWriter.Write("vooo")
      streamWriter.Write("\r\n")
      streamWriter.Write("--A300x--\r\n")
      streamWriter.Close()
    End Using
    ' response
    Using webResponse As HttpWebResponse = DirectCast(webRequest, HttpWebRequest).GetResponse()
      If webResponse.StatusCode() = HttpStatusCode.OK Then
        Using streamReader As New System.IO.StreamReader(webResponse.GetResponseStream())
          Throw New Exception(streamReader.ReadToEnd())
          streamReader.Close()
        End Using
      End If
      webResponse.Close()
    End Using

得到错误的请求(400)。访问令牌和网址已经过验证。有效。任何线索?

2 个答案:

答案 0 :(得分:2)

我使用以下方法将文件上传到Skydrive。工作正常,就像我一样。

 public void uploadFile(string ParentFolderID, string FileName)
    {
        string uri = String.Format(SkyDriveHelper.BaseURI + ParentFolderID + "/files"+"?access_token=" + account.accessInfo.access_token);
        try
        {
            WebClient client = new WebClient();
            client.Headers.Add("Content-Type", "multipart/form-data; boundary=A300x");
            Stream requestStream = client.OpenWrite(uri);

            //writing start boundary
            requestStream.Write("--A300x\r\n".ToByteArray(), 0, "--A300x\r\n".Count());

            //writing body headers
            string header1 = String.Format("Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\n", FileName);
            requestStream.Write(header1.ToByteArray(), 0, header1.Count());
            string header2 = "Content-Type: application/octet-stream\r\n";
            requestStream.Write(header2.ToByteArray(), 0, header2.Count());
            requestStream.Write("\r\n".ToByteArray(), 0, "\r\n".Count());

            //writing body - WRITE BYTES HERE 
            requestStream.Write("HelloWorld".ToByteArray(), 0, "HelloWorld".Count());

            //writing finish boundary
            requestStream.Write("\r\n\r\n".ToByteArray(), 0, "\r\n\r\n".Count());
            requestStream.Write("--A300x\r\n".ToByteArray(), 0, "--A300x\r\n".Count());
            requestStream.Close();

        }
        catch (Exception e)
        {
            MessageBox.Show("Download Error:" + e.Message);
        }

    }

答案 1 :(得分:0)

如果您查看响应正文(或使用Fiddler执行相同操作),您将找到一个JSON格式的错误消息,该消息可以准确解释问题所在。我的第一个猜测是该目标用户不存在 skydrive 文件夹。