我正在尝试通过 MS Graph API 将文件(.xlsx 小于 4MB)上传到驱动器位置。
请参阅下面的代码了解更多详细信息,我感谢您帮助解决当前基于 URL 面临的问题。错误 - 找不到该段的资源 好的。
public static string DecodeData(string encodedData)
{
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}
// Upload files to SharePoint
public void SharePointUploadDriveItems(string accessToken)
{
var httpClient = new HttpClient();
try
{
string path = @"C:\\StockLevelReportLocation\\New Files\\TestFileUpload.xlsx";
byte[] filebytes = System.IO.File.ReadAllBytes(path);
using (var client = new HttpClient())
{
string fileName = "TestFileToUpload.txt";
using (var request = new HttpRequestMessage(HttpMethod.Put, "https://graph.microsoft.com/v1.0/drives/root/Oks/Stock Report/TestFileUpload.xlsx/content"))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Headers.Add("Accept", "application/json;odata.metadata=verbose");
request.Content = new StringContent(DecodeData(Convert.ToBase64String(filebytes)), System.Text.Encoding.ASCII, "text/plain");
var driveItemsResponse = httpClient.SendAsync(request).Result;
var driveItemsResponseContent = driveItemsResponse.Content.ReadAsStringAsync().Result;
}
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
}
看起来您在 URL 路径的开头和结尾处缺少
:
。另一个问题是您没有指定驱动器 ID
您的网址
https://graph.microsoft.com/v1.0/drives/root/Oks/Stock Report/TestFileUpload.xlsx/content
但应该是
https://graph.microsoft.com/v1.0/drives/{drive_id}/root:/Oks/Stock Report/TestFileUpload.xlsx:/content
代码
using (var request = new HttpRequestMessage(HttpMethod.Put, "https://graph.microsoft.com/v1.0/drives/{drive_id}/root:/Oks/Stock Report/TestFileUpload.xlsx:/content"))
{
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
request.Headers.Add("Accept", "application/json;odata.metadata=verbose");
request.Content = new StringContent(DecodeData(Convert.ToBase64String(filebytes)), System.Text.Encoding.ASCII, "text/plain");
var driveItemsResponse = httpClient.SendAsync(request).Result;
var driveItemsResponseContent = driveItemsResponse.Content.ReadAsStringAsync().Result;
}