当我尝试调用 outlook restful api 时收到 "An action can only be invoked as a 'POST' request"
Getting "An action can only be invoked as a 'POST' request" when I am trying to call outlook restful api
目前正在使用 outlook api,虽然很辛苦,但我通常使用通过 Nuget 获取的 outlook 库;我已达到无法接受活动邀请的限制。因此,我着手 restful 向前景 api 发出呼吁。但是,当我打电话时,我收到以下消息 {"error":{"code":"InvalidMethod","message":"An action can only be invoked as a 'POST' request."}}称呼。
错误代码
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;
}
}
}
可能是你打电话的原因
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
希望对您有所帮助。
您的变量 request
包含一个您创建的 HttpRequestMessage
对象,但您的代码目前未对其执行任何操作。
尝试替换行
HttpResponseMessage hrm = await hc.GetAsync(requestUrl);
(正如另一个答案所指出的,提出了 GET
请求),
HttpResponseMessage hrm = await hc.SendAsync(request);
目前正在使用 outlook api,虽然很辛苦,但我通常使用通过 Nuget 获取的 outlook 库;我已达到无法接受活动邀请的限制。因此,我着手 restful 向前景 api 发出呼吁。但是,当我打电话时,我收到以下消息 {"error":{"code":"InvalidMethod","message":"An action can only be invoked as a 'POST' request."}}称呼。
错误代码
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;
}
}
}
可能是你打电话的原因
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
希望对您有所帮助。
您的变量 request
包含一个您创建的 HttpRequestMessage
对象,但您的代码目前未对其执行任何操作。
尝试替换行
HttpResponseMessage hrm = await hc.GetAsync(requestUrl);
(正如另一个答案所指出的,提出了 GET
请求),
HttpResponseMessage hrm = await hc.SendAsync(request);