我正在使用azure广告图api从活动目录中提取用户配置文件数据。我所有的输入参数都是正确的,令牌也是用下面的代码生成的。但它不会将用户配置文件对象作为response.response.IsSuccessStatusCode始终为false。这可能是我的错误?azure广告图api不拉用户信息
private readonly string graphUserUrl = "https://graph.windows.net/{0}/me?api-version=1.6"
string tenantName = "Microsoft.OnMicrosoft.com";
string authString = "https://login.microsoftonline.com/" + tenantName;
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
// Config for OAuth client credentials
ClientCredential clientCred = new ClientCredential(clientId, appKey);
string resource = "https://graph.windows.net";
string token = "";
try
{
AuthenticationResult authenticationResult = authenticationContext.AcquireToken(resource, clientCred);
token = authenticationResult.AccessToken;
}
catch (AuthenticationException ex)
{
}
UserProfile profile;
string requestUrl = String.Format(CultureInfo.InvariantCulture,graphUserUrl,HttpUtility.UrlEncode(tenantId));
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, requestUrl);
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
//HttpResponseMessage response = await client.SendAsync(request);
HttpResponseMessage response = client.SendAsync(request).Result;
// Return the user's profile in the view.
if (response.IsSuccessStatusCode)
{
string responseString = await response.Content.ReadAsStringAsync();
profile = JsonConvert.DeserializeObject<UserProfile>
(responseString);
}
你有解决这个问题?如果你仍然有问题,请随时告诉我。 –