如何使用MSAL连接到Onedrive?

时间:2016-07-26 19:10:12

标签: onedrive msal

我尝试使用MSAL令牌连接到OneDrive,但它返回错误=" invalid_token",error_description ="验证错误"

这是我的代码:

    public static string[] Scopes = { "User.Read", "Files.Read", "Sites.Read.All" };
    AuthenticationResult ar = await App.ClientApplication.AcquireTokenSilentAsync(Scopes);
    WelcomeText.Text = $"Welcome {ar.User.Name}"; //Login OK here

        //get data from API
        HttpClient client = new HttpClient();
        HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, "https://api.onedrive.com/v1.0/drives");
        message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", ar.Token);
        HttpResponseMessage response = await client.SendAsync(message);
        string responseString = await response.Content.ReadAsStringAsync();

任何人都知道我做错了什么?

2 个答案:

答案 0 :(得分:1)

直接API端点(api.onedrive.com)不支持从MSAL生成的访问令牌,只支持从MSA生成的令牌。如果您使用的是MSAL,则应使用Microsoft Graph API(graph.microsoft.com)为个人和企业用户访问OneDrive文件。

答案 1 :(得分:0)

你已经很久以前得到了答案,但我希望这个链接将来会对其他人有所帮助。 https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/resources/onedrive