如何使用新 API 将新文件上传到 OneDrive?
How to upload a new file to OneDrive using new API?
我正在尝试使用新的 OneDrive API 上传文件。我从简单的文件(即 < 100MB)开始,打算在简单的文件正常工作后继续进行可恢复上传!
我试过使用 http://onedrive.github.io/items/upload_put.htm,但我收到了 403。我认为这可能是因为该文件尚不存在,但我已经使用 OneDrive 网络上传文件 UI 成功,但代码仍然无法上传文件。
我正在使用的 URL 是这样的:
https://api.onedrive.com/v1.0/drive/root:/:/内容?access_token=
C#代码是:
using (Stream fileStream = await file.OpenStreamForReadAsync())
{
try
{
HttpStreamContent streamContent = new HttpStreamContent(fileStream.AsInputStream());
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, oneDriveURI);
request.Content = streamContent;
request.Content.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/octet-stream");
HttpResponseMessage response = await client.SendRequestAsync(request);
Debug.WriteLine("UploadFileToOneDrive: response = {0}", response.StatusCode);
}
catch (Exception ex)
{
Debug.WriteLine("UploadFileToOneDrive failed with exception {0}", ex.Message);
}
}
我得到了什么wrong/misunderstood? API 文档需要更多示例 :-(
403 错误代码与权限有关,因此您可能忘记包含上传文件的正确范围。当您发送 OAuth 请求时,您还需要将 "onedrive.readwrite" 作为范围之一。
GET https://login.live.com/oauth20_authorize.srf?client_id={client_id}&scope={scope}&response_type=token&redirect_uri={redirect_uri}
可以在“http://onedrive.github.io/auth/msa_oauth.htm”找到更多范围。希望对您有所帮助。
我正在尝试使用新的 OneDrive API 上传文件。我从简单的文件(即 < 100MB)开始,打算在简单的文件正常工作后继续进行可恢复上传!
我试过使用 http://onedrive.github.io/items/upload_put.htm,但我收到了 403。我认为这可能是因为该文件尚不存在,但我已经使用 OneDrive 网络上传文件 UI 成功,但代码仍然无法上传文件。
我正在使用的 URL 是这样的:
https://api.onedrive.com/v1.0/drive/root:/:/内容?access_token=
C#代码是:
using (Stream fileStream = await file.OpenStreamForReadAsync())
{
try
{
HttpStreamContent streamContent = new HttpStreamContent(fileStream.AsInputStream());
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, oneDriveURI);
request.Content = streamContent;
request.Content.Headers.ContentType = new Windows.Web.Http.Headers.HttpMediaTypeHeaderValue("application/octet-stream");
HttpResponseMessage response = await client.SendRequestAsync(request);
Debug.WriteLine("UploadFileToOneDrive: response = {0}", response.StatusCode);
}
catch (Exception ex)
{
Debug.WriteLine("UploadFileToOneDrive failed with exception {0}", ex.Message);
}
}
我得到了什么wrong/misunderstood? API 文档需要更多示例 :-(
403 错误代码与权限有关,因此您可能忘记包含上传文件的正确范围。当您发送 OAuth 请求时,您还需要将 "onedrive.readwrite" 作为范围之一。
GET https://login.live.com/oauth20_authorize.srf?client_id={client_id}&scope={scope}&response_type=token&redirect_uri={redirect_uri}
可以在“http://onedrive.github.io/auth/msa_oauth.htm”找到更多范围。希望对您有所帮助。