我正在尝试使用Microsoft Graph API通过REST调用创建可恢复的上传,我可以收到一个上传URL作为回报。但是,它根本不像文档URL,似乎是一个“较旧”的非Graph v2.0 API
https://dev.onedrive.com/items/upload_large_files.htm
在示例中,返回URL为
https://sn3302.up.1drv.com/up/fe6987415ace7X4e1eF866337
但是,我收到的是:
https://{server}/_api/v2.0/drive/items/01LFLHCDPPDY5LDTR3UREILVK4ISP2HJIE/uploadSession?guid='031a05ef-806e-4118-a5ff-8dea9b558c3e'&path='~tmp8B_test.xls'&overwrite=True&rename=False
这与OneDrive API差异一致。 https://dev.onedrive.com/direct-endpoint-differences.htm
但是导致401 Unauthorized响应,错误为
类型异常 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' 被扔了。“
我认为这是因为身份验证不同,当我在标题中放入'Authorization:Bearer {accesstoken}'时,我的MS-Graph访问令牌无效(该标题适用于所有我的其他REST调用通过Graph)
如何获取图表上传网址以将文件上传到OneDrive Business ?或如何让返回网址生效,以便我可以上传到OneDrive业务?
修改:显示权限
这是我创建访问令牌的地方
request.setEndpoint('https://login.microsoftonline.com/{tenantID}/oauth2/v2.0/token');
request.setHeader('Content-Type', 'application/x-www-form-urlencoded');
string body;
body = '&client_id={clientId}';
body += '&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default';
body += '&client_secret={Secret}';
body += '&grant_type=client_credentials';
我正在为上传会话进行API调用(忽略语法):
webRequest.setEndPoint('https://graph.microsoft.com/v1.0/users/{myUserId}/drive/items/{parentItem_folder_Id}:/test.xls:/createUploadSession');
webRequest.setHeader('Authorization', 'Bearer ' + getAccessToken());
webRequest.setHeader('Content-Type', 'application/json');
webRequest.setHeader('Accept', 'application/json');
webRequest.setHeader('Host', 'graph.microsoft.com');
webRequest.setHeader('Content-Length', '0');
webRequest.setMethod('POST');
答案 0 :(得分:1)
401错误是由于在尝试使用创建上传会话后返回的URL时使用“Authorization:Bearer”标头引起的。删除授权标头后,您可能仍会收到错误消息。对我来说这是403 Forbidden错误。
答案 1 :(得分:1)
我认为这里的问题是您在没有用户的情况下尝试上传文件(即仅限应用而非委派方案)。当您正在请求正确的范围(Files.ReadWrite.All
)时,目前不支持此方案。来自documentation:
注意:此API尚不支持Files.ReadWrite.All应用程序权限。计划很快得到全力支持。
目前,仅在委派方案中支持可恢复上传(即,用户已直接进行身份验证并将其上载到自己的驱动器中)。