【发布时间】:2020-03-11 14:28:31
【问题描述】:
我正在使用下面的代码调用来搜索共享点中的文档:
//ms graph 的app secret 和id azure
string cSecret = "XXXX";
string cId = "XXXXXXXX";
var scopes = new string[] { "https://graph.microsoft.com/.default" };
var confidentialClient = ConfidentialClientApplicationBuilder
.Create(cId)
.WithAuthority($"https://login.microsoftonline.com/murphyoil.onmicrosoft.com/v2.0")
.WithClientSecret(cSecret)
.Build();
string requestUrl;
GraphServiceClient graphClient =
new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
{
var authResult = await confidentialClient
.AcquireTokenForClient(scopes)
.ExecuteAsync();
// Add the access token in the Authorization header of the API request.
requestMessage.Headers.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
}));
requestUrl = "https://graph.microsoft.com/beta/search/query";
var searchRequest = new
{
requests = new[]
{
new
{
entityTypes = new[] {"microsoft.graph.driveItem"},
query = new
{
query_string = new
{
query = "policy AND filetype:docx"
}
},
from = 0,
size = 25
}
}
};
//construct a request
var message = new HttpRequestMessage(HttpMethod.Post, requestUrl);
var jsonPayload = graphClient.HttpProvider.Serializer.SerializeObject(searchRequest);
message.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
await graphClient.AuthenticationProvider.AuthenticateRequestAsync(message);
var response = await graphClient.HttpProvider.SendAsync(message);
//process response
var content = await response.Content.ReadAsStringAsync();
var result = JObject.Parse(content);
var searchItems = result["value"].First["hitsContainers"].First["hits"].Select(item =>
{
var itemUrl = (string)item["_source"]["webUrl"];
return itemUrl;
});
}
我正在使用上面的代码在 SharePoint 中搜索文档。 但获得未经授权的访问。相同的应用程序 ID 和密码在 MS 图形 Active Directory 搜索中有效,但在这种情况下不可用。
【问题讨论】:
-
这个问题有什么解决办法吗?
标签: c# sharepoint microsoft-graph-api