无法访问 onedrive 内容



我正在开发一个应用程序,我需要在其中使用 Microsoft Graph 来访问 OneDrive for Business 上的文件。我在 Azure 上创建了一个 Web 应用程序,并设法获取了身份验证令牌,并且能够使用https://graph.microsoft.com/v1.0/me检索用户信息。但是,如果我尝试获取 OneDrive 的内容https://graph.microsoft.com/v1.0/me/drive/root/children则会收到拒绝访问错误。

我已经检查了图形资源管理器,并且能够毫无问题地获得查询结果。对于我的 Web 应用,我使用以下图形权限:

  • 人员阅读 - 委派
  • Sites.ReadWrite.All - 委派
  • Sites.ReadWrite.All - Application
  • 任务.读写 - 委派
  • User.Export.All -委派
  • 用户.导出.全部 - 应用程序
  • 用户邀请全部 - 委派
  • 用户.邀请.全部 - 应用程序
  • 用户读取 - 委派
  • User.Read.All - 委派
  • 用户.全部读取 - 应用程序
  • User.ReadBasic.All - 委派
  • 用户读写 - 委派
  • User.ReadWrite.All - 委派
  • User.ReadWrite.All - Application
  • offline_access - 委派
  • openid - 委托
public async Task<string> GetTokenAsync()
{
HttpResponseMessage resp;
using(var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var req = new HttpRequestMessage(HttpMethod.Post,
$"https://login.microsoftonline.com/{tenant}/oauth2/token/");
req.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{ { "grant_type", "password" },
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "resource", "https://graph.microsoft.com" },
{ "username", username },
{ "password", password },
{ "scope", "https%3A%2F%2Fgraph.microsoft.com%2F.default" }
});
resp = await httpClient.SendAsync(req);
string content = await resp.Content.ReadAsStringAsync();
var jsonObj = new JavaScriptSerializer().Deserialize<dynamic>(content);
string token = jsonObj["access_token"];
Console.WriteLine(token);
return token;
}
}
public async Task<string> SendGraphRequest(string requestUrl)
{
using(HttpClient httpClient = new HttpClient())
{
// Set up the HTTP GET request
HttpRequestMessage apiRequest =
new HttpRequestMessage(HttpMethod.Get, requestUrl);
apiRequest.Headers.UserAgent
.Add(new ProductInfoHeaderValue("OAuthStarter", "2.0"));
apiRequest.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", await GetTokenAsync());
// Send the request and return the response
HttpResponseMessage response = await httpClient.SendAsync(apiRequest);
var s = response.Content.ReadAsStringAsync();
Console.WriteLine(s.Result);
return s.Result;
}
}

我对图形 API 的调用是:

SendGraphRequest("https://graph.microsoft.com/v1.0/me/drive").Wait();

我从中得到的结果是:

{
"error": {
"code": "accessDenied",
"message": "There has been an error authenticating the request.",
"innerError": {
"request-id": "request-id",
"date": "2019-09-24T11:03:29"
}
}
}

你在这里过度配置了你的范围。例如,您不需要读取和读写范围。你还请求了一些应用程序范围,但使用的是 OAuth 密码授予,该授予仅适用于委派范围(不能混合使用两者(。

我建议将范围与以下委派权限配对:

openidUser.ReadWriteUser.ReadBasic.AllFiles.ReadWrite.All
People.ReadSites.ReadWrite.AllTasks.ReadWriteoffline_access

请注意,Sites.ReadWrite.AllFiles.ReadWrite.All有点不同。一个涉及SharePoint,另一个涉及OneDrive。

在尝试解决此问题将近两周后,我意识到问题不在于代码,而在于 azure 配置。我假设如果网络应用程序安装在活动目录管理员的帐户上,则该活动目录上的所有用户都将能够使用相同的网络应用程序访问其各自的One Drive帐户。不幸的是,情况并非如此,如果用户(不是管理员(尝试使用 Web 应用程序访问 One Drive,他或她唯一能够访问的是他们的帐户信息,无法访问文件。若要使用图形 API 访问文件,需要将 Web 应用安装在其各自的活动目录中。

最新更新