GraphServiceClient – 如何定义请求体?

huangapple go评论80阅读模式
英文:

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(&quot;AZ_APPLICATION_CLIENT_ID&quot;);
            var ApplicationClientSecret = Environment.GetEnvironmentVariable(&quot;AZ_CLIENT_SECRET&quot;);
            var AzureTenantID = Environment.GetEnvironmentVariable(&quot;AZ_TENANT&quot;);
  
            string[] scopes = new[] { &quot;https://graph.microsoft.com/.default&quot; };
            
            // 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[&quot;&lt;myguid&gt;&quot;].GetAsync();
            return result;


Now I want to replace the last 2 lines with a call to Microsoft Search api.  I&#39;ve tested first via ms graph explorer like so: 

[![enter image description here][1]][1]


It gives me the results I&#39;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(&quot;AZ_APPLICATION_CLIENT_ID&quot;);
            var ApplicationClientSecret = Environment.GetEnvironmentVariable(&quot;AZ_CLIENT_SECRET&quot;);
            var AzureTenantID = Environment.GetEnvironmentVariable(&quot;AZ_TENANT&quot;);
  
            string[] scopes = new[] { &quot;https://graph.microsoft.com/.default&quot; };
            
            // 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&lt;SearchRequest&gt;
    	    {
    			new SearchRequest
    	    		{
    				EntityTypes = new List&lt;EntityType?&gt;
    				{
    					EntityType.DriveItem,
    				},
    				Query = new SearchQuery
    				{
    					QueryString = &quot;some search criteria&quot;,
    				},
    				From = 0,
    				Size = 35,
    				QueryAlterationOptions = new SearchAlterationOptions
    				{
    					EnableSuggestion = true,
    					EnableModification = true,
    		    	},
    	     	},
         	},
         };
    var result = await graphClient.Search.Query.PostAsync(requestBody);

But I&#39;m getting some compile errors. 

**Versions**

[![enter image description here][2]][2]

**library references**

[![enter image description here][3]][3]

**Problems**

The error I&#39;m coming across is &quot;The type or namespace name &#39;Beta&#39; does not exist in the namespace &#39;Microsoft.Graph&#39; (are you missing an assembly reference?)&quot;

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, &quot;https://graph.microsoft.com/beta/&quot;);

But it&#39;s not clear how I should change the call to Microsoft.Graph.Beta.Search.Query.QueryPostRequestBody because I don&#39;t have that method in the GraphServiceClient I have created. 

So I&#39;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, &quot;https://graph.microsoft.com/v1.0/&quot;);
                                    
  var requestBody = new Microsoft.Graph.Search.Query.QueryPostRequestBody

huangapple
  • 本文由 发表于 2023年7月18日 05:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定