我想通过微软的Graph API从Azure Active Directory中读取用户的数据。使用图形浏览器,我可以获得所有用户,但使用独立应用程序后,我收到令牌后最终会收到“未经授权”的响应。我显然错过了一些步骤,但对我而言,这不是很明显。任何有识之士将不胜感激Microsoft Graph Api令牌无效或未经授权?
下面的代码是基于断MSFT样本:
// config values
// authority = "https://login.microsoftonline.com/{ TENANT ID }/oauth2/"
// resource uri = "https:// APP NAME .azurewebsites.net";
// graph uri = https://graph.windows.net/TENANT ID/ also tried https://graph.windows.net/v1.0/
// short form
public async void GetUsers(ADConfiguration config)
{
_authContext = new AuthenticationContext(config.GetAuthority());
_clientCredential = new ClientCredential(config.ClientId, config.ClientSecret);
AuthenticationResult result = null;
// obtain the token, this part is still successful
result = await _authContext.AcquireTokenAsync(config.ResourceUri, _clientCredential);
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
string address = config.GetGraphiUri() + "users?api-version=1.6";
// this response is always unauthorized
HttpResponseMessage response = await _httpClient.GetAsync(address);
}
感谢您的回答。建议将错误消息从“未授权”更改为“禁止”。一个横向移动我猜... 我相信资源uri是指Azure上的AD服务器。无论哪种方式,我尝试过任何变化没有太多的成功。 – Mark
禁止表示您的应用程序没有必要的权限。检查是否已将它们添加到Azure AD Graph。 – juunas
我们有两个可以访问的API:'Windows Azure Active Directory'和'Microsoft Graph',它们都有权限执行所有操作。我已经在寻找'Azure AD Graph'来添加API访问权限,但是这不会作为选项提供。 – Mark