当我试图调用outlook restful api时,获取“一个动作只能作为'POST'请求调用”

时间:2017-01-18 21:01:11

标签: c# rest office365 outlook-api

目前使用outlook api,我甚至很难处理通过Nuget获得的outlook库;我已达到限制,我无法接受活动邀请。所以我继续打电话给outlook api。但是,当我拨打电话时,我收到以下消息{“error”:{“code”:“InvalidMethod”,“message”:“操作只能作为'POST'请求调用。”}}当执行呼叫。

错误代码

 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;
            }
        }

    }

2 个答案:

答案 0 :(得分: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

希望这对你有所帮助。

答案 1 :(得分:1)

您的变量request包含您创建的HttpRequestMessage对象,但您的代码目前尚未对其执行任何操作。

尝试替换

                HttpResponseMessage hrm = await hc.GetAsync(requestUrl);

(正如另一个答案所指出的那样,发出GET请求),

                HttpResponseMessage hrm = await hc.SendAsync(request);