错误:未经身份验证,必须经过身份验证才能使用“/驱动器”语法 - 使用rest api(图形)获取项目时

时间:2021-01-18 07:03:15

标签: c# onedrive azure-ad-graph-api msal

我正在使用 Windows 应用程序从 onedrive api 上传和下载文件。

<块引用>

获取token的代码(此代码是在创建应用注册后直接从azure门户下载的)

    string graphAPIEndpoint = "https://graph.microsoft.com/v1.0/me";
    string[] scopes = new string[] { "user.read" };
    AuthenticationResult authResult = null;
    
    var app = App.PublicClientApp;
    ResultText.Text = string.Empty;
    TokenInfoText.Text = string.Empty;
    var accounts = await app.GetAccountsAsync();
    var firstAccount = accounts.FirstOrDefault();
    try
    {
             authResult = await app.AcquireTokenSilent(scopes, firstAccount)
               .ExecuteAsync();
    }
    catch (MsalUiRequiredException ex)
    {
                        System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
                        try
                        {
                            authResult = await app.AcquireTokenInteractive(scopes)
                                .WithAccount(firstAccount)
                                .WithParentActivityOrWindow(new WindowInteropHelper(this).Handle) // optional, used to center the browser on the window
                                .WithPrompt(Prompt.SelectAccount)
                                .ExecuteAsync();
                        }
                        catch (MsalException msalex)
                        {
                            ResultText.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}";
                        }
}
<块引用>

这是从onedrive api获取项目下载地址的代码

string url="https://graph.microsoft.com/v1.0/me/drive/root:/Qwerty/test.txt";
string token=authResult.AccessToken;
var httpClient = new System.Net.Http.HttpClient();
            System.Net.Http.HttpResponseMessage response;
            try
            {
                var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url);
                //Add the token in Authorization header
                request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
                response = await httpClient.SendAsync(request);
                var content = await response.Content.ReadAsStringAsync();
                if (response.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    Response cls = new Response();
                    cls.Success = "TRUE";
                    cls.Method = "GetAllFiles";
                    cls.Data = content;
                    return cls;
                }
                else
                {
                    Response cls = new Response();
                    cls.Success = "FALSE";
                    cls.Method = "GetAllFiles";
                    cls.Data = content;
                    return cls;
                }

我收到此错误“必须经过身份验证才能使用‘/drive’语法”。此应用程序适用于我的个人应用程序注册之一。但是当我使用下面的应用程序注册时,它会显示此错误。我按照完全相同的步骤创建应用程序注册我不知道为什么会出现这个错误。 有错误的客户端 ID:463921cd-72a3-495d-847e-259b99dda89e 请帮帮我 This is the sreenshot

2 个答案:

答案 0 :(得分:0)

如果您想下载 DriveItem 的内容,您必须添加以下权限之一来调用此 API。并且在使用/me时需要添加委托权限。您的代码中的范围也经过许可更改。

enter image description here

那你可以参考代码示例:

带有图形 SDK 的 C#:https://stackoverflow.com/a/63806689/13308381


尝试在 Postman 中关注 these steps

1.请求授权码

GET https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?client_id={client_id}&scope={scope}
  &response_type=code&redirect_uri={redirect_uri}

2.请求访问令牌

POST https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token

client_id={client_id}&scope={scope}&redirect_uri={redirect_uri}&client_secret={client_secret}
&code={code}&grant_type=authorization_code

3.调用 Microsoft Graph API

GET /me/drive/root:/{item-path}:/content

答案 1 :(得分:0)

我已经发现了这个问题。问题是在创建访问令牌时,我们需要指定范围,如(Files.Read、Files.ReadWrite 等(添加我们需要的任何内容)),然后使用相同的令牌下载文件,就可以了

相关问题