【发布时间】:2020-12-16 16:41:25
【问题描述】:
我做错了什么?我想列出 OneDrive 根目录中的文件。但我总是得到 401 Unauthorized。
我使用 Fiddler 来跟踪请求,并且请求 OAuth 令牌似乎工作正常。但是,当我尝试请求 https://graph.microsoft.com/v1.0/me/drive/root/children 时,我得到 Unauthorized 作为响应,代码为 UnknownError
private static GraphServiceClient GetAuthenticatedGraphClient()
{
List<string> scopes = new List<string>
{
"https://graph.microsoft.com/.default",
};
var cca = ConfidentialClientApplicationBuilder.Create(CLIENT_ID)
.WithAuthority(AadAuthorityAudience.PersonalMicrosoftAccount)
.WithClientSecret(SECRET)
.Build();
GraphServiceClient graphServiceClient =
new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
{
// Retrieve an access token for Microsoft Graph (gets a fresh token if needed).
var authResult = await cca.AcquireTokenForClient(scopes).ExecuteAsync();
// Add the access token in the Authorization header of the API
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
})
);
return graphServiceClient;
}
var drive = GraphClient.Me.Drive.Root.Children.
Request().
GetAsync();
仅供参考:我正在使用 .NET MVC 5,我想在没有用户交互的情况下访问我的 个人 onedrive。我似乎对我应该为此使用什么流程感到有点迷茫。
【问题讨论】:
-
我会检查jwt.ms 中的令牌,看看您是否拥有必要的权限/角色/范围。
-
你的应用是什么类型的?它是网络应用还是控制台应用?
-
它是.NET MVC 应用程序(.NET Framework)
标签: c# azure-active-directory microsoft-graph-api