英文:
GraphServiceClient - how to define the request body?
问题
**背景**
我有一个 Azure 函数应用程序,其中包括以下逻辑部分:
```csharp
var ApplicationClientID = Environment.GetEnvironmentVariable("AZ_APPLICATION_CLIENT_ID");
var ApplicationClientSecret = Environment.GetEnvironmentVariable("AZ_CLIENT_SECRET");
var AzureTenantID = Environment.GetEnvironmentVariable("AZ_TENANT");
string[] scopes = new[] { "https://graph.microsoft.com/.default" };
// using Azure.Identity;
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
// using https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
AzureTenantID, ApplicationClientID, ApplicationClientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var result = await graphClient.Users["<myguid>"].GetAsync();
return result;
现在我想将最后两行替换为调用 Microsoft Search API。我已经通过 Microsoft Graph Explorer 进行了测试,如下所示:
[![在这里输入图片描述][1]][1]
它给我提供了我正在寻找的结果。所以我将 C# 代码片段从 Graph Explorer 复制到我的项目中。
现在代码看起来像这样:
var ApplicationClientID = Environment.GetEnvironmentVariable("AZ_APPLICATION_CLIENT_ID");
var ApplicationClientSecret = Environment.GetEnvironmentVariable("AZ_CLIENT_SECRET");
var AzureTenantID = Environment.GetEnvironmentVariable("AZ_TENANT");
string[] scopes = new[] { "https://graph.microsoft.com/.default" };
// using Azure.Identity;
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
// using https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
AzureTenantID, ApplicationClientID, ApplicationClientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
// ****** 这是 MS Search API 逻辑的起始位置 ******** //
var requestBody = new Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody
{
Requests = new List<SearchRequest>
{
new SearchRequest
{
EntityTypes = new List<EntityType?>
{
EntityType.DriveItem,
},
Query = new SearchQuery
{
QueryString = "some search criteria",
},
From = 0,
Size = 35,
QueryAlterationOptions = new SearchAlterationOptions
{
EnableSuggestion = true,
EnableModification = true,
},
},
},
};
var result = await graphClient.Search.Query.PostAsync(requestBody);
但是我遇到了一些编译错误。
版本
[![在这里输入图片描述][2]][2]
库引用
[![在这里输入图片描述][3]][3]
问题
我遇到的错误是“命名空间 'Microsoft.Graph' 中不存在 'Beta' 类型或命名空间 (是否缺少程序集引用?)”
出现在以下这一行:
var requestBody = new Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody
我添加了 baseUrl 到 graphClient,以便指向 beta,然后更改了 requestBody 的赋值。
var graphClient = new GraphServiceClient(clientSecretCredential, scopes, "https://graph.microsoft.com/beta/");
但是不清楚我应该如何更改对 Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody 的调用,因为我创建的 GraphServiceClient 中没有这个方法。
所以我尝试做类似于这样的事情:
[![在这里输入图片描述][4]][4]
Graph Explorer 中的注释指出所有代码片段都基于我拥有的 5.x 版本。如果有任何提示,将不胜感激。
[1]: https://i.stack.imgur.com/zEftA.png
[2]: https://i.stack.imgur.com/ryTTv.jpg
[3]: https://i.stack.imgur.com/6mUbU.png
[4]: https://i.stack.imgur.com/HWDna.png
<details>
<summary>英文:</summary>
**Background**
I have an azure function app that in part, includes the following logic:
var ApplicationClientID = Environment.GetEnvironmentVariable("AZ_APPLICATION_CLIENT_ID");
var ApplicationClientSecret = Environment.GetEnvironmentVariable("AZ_CLIENT_SECRET");
var AzureTenantID = Environment.GetEnvironmentVariable("AZ_TENANT");
string[] scopes = new[] { "https://graph.microsoft.com/.default" };
// using Azure.Identity;
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
// using https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
AzureTenantID, ApplicationClientID, ApplicationClientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
var result = await graphClient.Users["<myguid>"].GetAsync();
return result;
Now I want to replace the last 2 lines with a call to Microsoft Search api. I've tested first via ms graph explorer like so:
[![enter image description here][1]][1]
It gives me the results I'm looking for. So I copied the c# code snippet from Graph Explorer into my project.
The code now looks like this:
var ApplicationClientID = Environment.GetEnvironmentVariable("AZ_APPLICATION_CLIENT_ID");
var ApplicationClientSecret = Environment.GetEnvironmentVariable("AZ_CLIENT_SECRET");
var AzureTenantID = Environment.GetEnvironmentVariable("AZ_TENANT");
string[] scopes = new[] { "https://graph.microsoft.com/.default" };
// using Azure.Identity;
var options = new ClientSecretCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
};
// using https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
AzureTenantID, ApplicationClientID, ApplicationClientSecret, options);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
//****** THIS IS WHERE THE MS SEARCH API LOGIC STARTS ********//
var requestBody = new Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody
{
Requests = new List<SearchRequest>
{
new SearchRequest
{
EntityTypes = new List<EntityType?>
{
EntityType.DriveItem,
},
Query = new SearchQuery
{
QueryString = "some search criteria",
},
From = 0,
Size = 35,
QueryAlterationOptions = new SearchAlterationOptions
{
EnableSuggestion = true,
EnableModification = true,
},
},
},
};
var result = await graphClient.Search.Query.PostAsync(requestBody);
But I'm getting some compile errors.
**Versions**
[![enter image description here][2]][2]
**library references**
[![enter image description here][3]][3]
**Problems**
The error I'm coming across is "The type or namespace name 'Beta' does not exist in the namespace 'Microsoft.Graph' (are you missing an assembly reference?)"
on this line:
var requestBody = new Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody
I added a baseUrl to the graphClient so it points to beta and then change the requestBody assignment.
var graphClient = new GraphServiceClient(clientSecretCredential, scopes, "https://graph.microsoft.com/beta/");
But it's not clear how I should change the call to Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody because I don't have that method in the GraphServiceClient I have created.
So I'm trying to do something like this:
[![enter image description here][4]][4]
The note in Graph Explorer indicates that the code snippets are all based on 5.x which I have.
Any tips would be appreciated.
[1]: https://i.stack.imgur.com/zEftA.png
[2]: https://i.stack.imgur.com/ryTTv.jpg
[3]: https://i.stack.imgur.com/6mUbU.png
[4]: https://i.stack.imgur.com/HWDna.png
</details>
# 答案1
**得分**: 0
根据上面建议的评论,我更改了逻辑,不再使用 beta:
```csharp
var graphClient = new GraphServiceClient(clientSecretCredential, scopes, "https://graph.microsoft.com/v1.0/");
var requestBody = new Microsoft.Graph.Search.Query.QueryPostRequestBody
英文:
Per the suggested comments above, I changed the logic to not use beta:
var graphClient = new GraphServiceClient(clientSecretCredential, scopes, "https://graph.microsoft.com/v1.0/");
var requestBody = new Microsoft.Graph.Search.Query.QueryPostRequestBody
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论