0

目前正在使用outlook api,即使很艰难,我通常使用通过Nuget 获得的outlook 库;我已达到无法接受活动邀请的限制。因此,我继续对 Outlook api 进行安静的调用。但是,当我拨打电话时,我收到以下消息 {"error":{"code":"InvalidMethod","message":"An action can only be invoked as a 'POST' request."}} when执行调用。

错误代码

 class Program
    {
        static void Main(string[] args)
        {
                var testAccept = ExecuteClientCall.AcceptEvent().Result; 
        }

 public static async Task<bool> AcceptEvent()
        {
            AuthenticationContext authenticationContext = new AuthenticationContext(CrmPrototype.Helpers.AuthHelper.devTenant);
            try
            {
                var token = await GetTokenHelperAsync(authenticationContext, CrmPrototype.Helpers.AuthHelper.OutlookAuthenticationEndpoint);
                string requestUrl = "https://outlook.office.com/api/v2.0/Users/***@nowwhere.com/events('AAQkAGZiNDQxZTVkLWQzZjEtNDdjNy04OTc4LTM4NmNjM2JiOTRjNAAQAFpV0CnWR0FIpWFYRtszPHU=')/accept";

                HttpClient hc = new HttpClient();
                hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

                var method = new HttpMethod("POST");

                var request = new HttpRequestMessage(method, requestUrl)
                {
                    Content = new StringContent("{SendResponse: true}", Encoding.UTF8, "application/json")
                };

                HttpResponseMessage hrm = await hc.GetAsync(requestUrl);



                if (hrm.IsSuccessStatusCode)
                {
                    string jsonresult = await hrm.Content.ReadAsStringAsync();
                    var stophere = 0;

                }
                else
                {
                    return false;
                }
                return true;
            }

            catch (Exception ex)
            {
                throw;
            }
        }

    }
4

2 回答 2

1

也许原因是你打电话

hc.GetAsync(requestUrl);

医生说这种方法:

Sends a GET request to the specified Uri as an asynchronous operation.

尝试:

PostAsync(Uri, HttpContent)

https://msdn.microsoft.com/en-us/library/system.net.http.httpclient(v=vs.118).aspx

希望这对您有所帮助。

于 2017-01-18T22:54:01.873 回答
1

您的变量request包含 HttpRequestMessage您创建的对象,但您的代码目前不使用它做任何事情。

换线试试

                HttpResponseMessage hrm = await hc.GetAsync(requestUrl);

(正如另一个答案所指出的那样,它提出了一个GET请求),与

                HttpResponseMessage hrm = await hc.SendAsync(request);
于 2017-01-18T23:07:59.733 回答