我正在尝试通过Microsofts的Graph API从Azure Active Directory读取用户的数据。使用Graph Explorer我可以获得所有用户但是使用独立应用程序我收到令牌后最终会收到“未经授权”的响应。我显然错过了一些步骤,但对我来说这不是什么明显的步骤。任何见解都将不胜感激
以下代码基于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);
}
答案 0 :(得分:1)
除了回答你的新问题。从您的代码中,您使用client credential flow获取令牌。在客户端凭据流中,权限直接授予应用程序本身。
由于您使用的是Azure AD Graph API,因此需要添加application permission
:
Settings
Required permissions
部分,选择Windows Azure Active Directory
(Azure广告图API),添加应用所需的相关应用权限。Grant Permissions
以获得管理员凭据的管理员同意。答案 1 :(得分:0)
您的配置值似乎已关闭:
权限应为:https://login.microsoftonline.com/{TENANT ID}
。
在我看来,您正在尝试使用Azure AD Graph API,而不是Microsoft Graph API。
在那种情况下:
资源URI 应为:https://graph.windows.net/
。 (MS Graph API为https://graph.microsoft.com/
)