1
我想上傳一個圖像文件(12 Kb大小)管理帳戶onedrive使用Web API的業務。 我可以得到沒有錯誤的驅動器,根和childrenItems。使用微軟圖上傳文件到onedrive業務使用微軟圖
var drive = FilesHelper.GetUserPersonalDrive();
var root = FilesHelper.GetUserPersonalDriveRoot();
var childrenItems = FilesHelper.ListFolderChildren(drive.Id, root.Id);
但是當我嘗試上傳的圖片文件:
var new FileOnRoot= UploadSampleFile(drive,root,Server.MapPath("~/drive.png"));
拋出異常:
{
"error": {
"code": "unauthenticated",
"message": "The caller is not authenticated.",
"innerError": {
"request-id": "1bd87259-4eef-4c36-8356-2f8fcc274608",
"date": "2016-12-01T10:35:50"
}
}
}
我允許幾乎下圖形API授權權限的所有閱讀權限和所有的應用程序權限。
private DriveItem UploadSampleFile(Drive drive, DriveItem newFolder, String filePath)
{
DriveItem result = null;
Stream memPhoto = getFileContent(filePath);
try
{
if (memPhoto.Length > 0)
{
String contentType = "image/png";
result = FilesHelper.UploadFileDirect(drive.Id, newFolder.Id,
new DriveItem
{
File = new File { },
Name = filePath.Substring(filePath.LastIndexOf("\\") + 1),
ConflictBehavior = "rename",
},
memPhoto,
contentType);
}
}
catch (Exception ex)
{
}
FilesHelper類
public static class FilesHelper
{
public static Drive GetUserPersonalDrive()
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}me/drive",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
var drive = JsonConvert.DeserializeObject<Drive>(jsonResponse);
return (drive);
}
public static DriveItem GetUserPersonalDriveRoot()
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}me/drive/root",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
var folder = JsonConvert.DeserializeObject<DriveItem>(jsonResponse);
return (folder);
}
public static List<DriveItem> ListFolderChildren(String driveId, String folderId, Int32 numberOfItems = 100)
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}drives/{1}/items/{2}/children?$top={3}",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
folderId,
numberOfItems));
var driveItems = JsonConvert.DeserializeObject<DriveItemList>(jsonResponse);
return (driveItems.DriveItems);
}
public static Stream GetFileContent(String driveId, String fileId, String contentType)
{
Stream fileContent = MicrosoftGraphHelper.MakeGetRequestForStream(
String.Format("{0}drives/{1}/items/{2}/content",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
fileId),
contentType);
return (fileContent);
}
public static DriveItem UploadFileDirect(String driveId, String parentFolderId,
DriveItem file, Stream content, String contentType)
{
var jsonResponse = MicrosoftGraphHelper.MakePutRequestForString(
String.Format("{0}drives/{1}/items/{2}/children/{3}/content",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
parentFolderId,
file.Name),
content,
contentType);
var uploadedFile = JsonConvert.DeserializeObject<DriveItem>(jsonResponse);
return (uploadedFile);
}
}
MicrosoftGraphHelper類
public static class FilesHelper
{
public static Drive GetUserPersonalDrive()
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}me/drive",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
var drive = JsonConvert.DeserializeObject<Drive>(jsonResponse);
return (drive);
}
public static DriveItem GetUserPersonalDriveRoot()
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}me/drive/root",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri));
var folder = JsonConvert.DeserializeObject<DriveItem>(jsonResponse);
return (folder);
}
public static List<DriveItem> ListFolderChildren(String driveId, String folderId, Int32 numberOfItems = 100)
{
String jsonResponse = MicrosoftGraphHelper.MakeGetRequestForString(
String.Format("{0}drives/{1}/items/{2}/children?$top={3}",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
folderId,
numberOfItems));
var driveItems = JsonConvert.DeserializeObject<DriveItemList>(jsonResponse);
return (driveItems.DriveItems);
}
public static Stream GetFileContent(String driveId, String fileId, String contentType)
{
Stream fileContent = MicrosoftGraphHelper.MakeGetRequestForStream(
String.Format("{0}drives/{1}/items/{2}/content",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
fileId),
contentType);
return (fileContent);
}
public static DriveItem UploadFileDirect(String driveId, String parentFolderId,
DriveItem file, Stream content, String contentType)
{
var jsonResponse = MicrosoftGraphHelper.MakePutRequestForString(
String.Format("{0}drives/{1}/items/{2}/children/{3}/content",
MicrosoftGraphHelper.MicrosoftGraphV1BaseUri,
driveId,
parentFolderId,
file.Name),
content,
contentType);
var uploadedFile = JsonConvert.DeserializeObject<DriveItem>(jsonResponse);
return (uploadedFile);
}
}