【问题标题】:Unable to get access to onedrive content无法访问 onedrive 内容
【发布时间】:2019-09-24 11:57:19
【问题描述】:

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

我已经在 Graph Explorer 上进行了检查,并且能够毫无问题地获得查询结果。对于我的网络应用,我使用以下图表权限:

  • People.Read - 委托
  • Sites.ReadWrite.All - 委托
  • Sites.ReadWrite.All - 应用程序
  • Tasks.ReadWrite - 委托
  • User.Export.All -Delegated
  • User.Export.All - 应用程序
  • User.Invite.All - 委托
  • User.Invite.All - 应用程序
  • User.Read - 委托
  • User.Read.All - 委托
  • User.Read.All - 应用程序
  • User.ReadBasic.All - 委托
  • User.ReadWrite - 委托
  • User.ReadWrite.All - 委托
  • User.ReadWrite.All - 应用程序
  • 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"
    }
  }
}

【问题讨论】:

    标签: microsoft-graph-api onedrive


    【解决方案1】:

    您在这里过度配置了您的范围。例如,您不需要 both Read 和 ReadWrite 范围。您还请求一些应用程序范围,但使用 OAuth 密码授权,该授权仅适用于委托范围(您不能将两者混合使用)。

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

    openid User.ReadWrite User.ReadBasic.All Files.ReadWrite.All
    People.Read Sites.ReadWrite.All Tasks.ReadWrite offline_access

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

    【讨论】:

    • 我按照你的建议做了,只委托了范围,只保留了你提供的列表中的那些。我一直得到相同的结果。对于https://graph.microsoft.com/v1.0/me,我设法获取了用户数据,但是一旦我尝试像https://graph.microsoft.com/v1.0/me/drive/root/children 这样的操作,我就会收到拒绝访问错误。
    • 你能在浏览器中打开 OneDrive 的 UI 吗?如果您可以将实际的 access_token 添加到您的问题中,这可能会有所帮助。
    • 是的,我可以在浏览器中打开 OneDrive 的 UI。
    • 我得到的token是:justpaste.it/3q446(使用这个网站是因为token太大)
    • 那个标记看起来是正确的。您是否尝试过使用 SDK 而不是手写调用?
    【解决方案2】:

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

    【讨论】:

    • 嗨@sion005,抱歉将近一年后。我不是管理员,我不断收到拒绝访问。你是说我必须安装 Azure AD 应用程序?
    猜你喜欢
    • 2013-04-21
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    • 2020-06-05
    相关资源
    最近更新 更多