【问题标题】:C# Graph API TranslateExchangeIds().PostAsync() HTTP Method not allowedC# Graph API TranslateExchangeIds().PostAsync() HTTP 方法不允许
【发布时间】:2022-01-18 09:27:37
【问题描述】:

我正在使用 C# 包到 Microsoft Graph API。我可以从 Graph API 读取消息。现在我想翻译如下所示的消息 ID: https://docs.microsoft.com/de-de/graph/api/user-translateexchangeids?view=graph-rest-1.0&tabs=csharp

var translatedIds = client.Users[firstMailboxElement.SourcePostbox]
  .TranslateExchangeIds(toBeTranslated, ExchangeIdFormat.RestImmutableEntryId, ExchangeIdFormat.RestId)
  .Request()
  .PostAsync()
  .Result;

当我这样做时,我得到以下异常:

System.AggregateException
One or more errors occurred. 
(Code: Request_BadRequest Message: Specified HTTP method is not allowed for the request target. 
Inner error: AdditionalData: date: 2021-12-15T06:52:45 [...])

这似乎没有意义,因为我无法更改 HTTP 方法。

任何想法如何解决这个问题?

【问题讨论】:

    标签: c# asp.net-core microsoft-graph-api


    【解决方案1】:
    var scopes = new[] { "https://graph.microsoft.com/.default" };
    var tenantId = "your_tenant_name.onmicrosoft.com";
    var clientId = "azure_ad_app_client_id";
    var clientSecret = "client_secret";
    var options = new TokenCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
    };
    var clientSecretCredential = new ClientSecretCredential(
        tenantId, clientId, clientSecret, options);
    var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
    var inputIds = new List<String>()
    {
        "asdf"
    };
    var sourceIdType = ExchangeIdFormat.RestId;
    var targetIdType = ExchangeIdFormat.RestImmutableEntryId;
    var res = graphClient.Users["user_id"].TranslateExchangeIds(inputIds, targetIdType, sourceIdType).Request().PostAsync();
    var a = res.Result;
    

    【讨论】:

    • 究竟应该修复什么?您也调用 PostAsync() 但它返回不同的异常。
    【解决方案2】:

    当异常发生时,我发现firstMailboxElement.SourcePostbox 为空。 所以调用到client.Users[null],所以请求的URL不完整。

    【讨论】:

    • 我的测试结果是因为我使用了错误的inputIds作为输入参数。这没有意义,因为它可以成功获得响应,我想在我的帖子中展示我如何设置请求,你可以比较你和我的。无论如何,你找到了问题,恭喜。
    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 1970-01-01
    • 2018-11-11
    • 2018-12-20
    • 2020-05-04
    • 2012-06-29
    • 2016-08-05
    • 2016-03-08
    相关资源
    最近更新 更多