2015-02-06 61 views
1

这里是下面的代码段失败的异常有关的响应与状态代码获取消息404:错误404尝试使用OutlookServicesClient CLAS

  var client = new Microsoft.Office365.OutlookServices 
       .OutlookServicesClient(GetAPIEndpointUri(), 
       GetOrPrepareApplicationAccessTokenAsync 
       ); 
      var messageFetcher = client.Users[mailbox].Messages.GetById(itemId); 
      var message = await messageFetcher.ExecuteAsync(); 
      message.Subject = subject; 
      await message.UpdateAsync(); 

其实下面的行失败:

  var message = await messageFetcher.ExecuteAsync(); 

查看异常详细信息中的查询URI,它看起来像这样:

https://outlook.office365.com/api/Users('mailbox%40domain.onmicrosoft.com')/ Messages('long message

{"message":"An error has occurred.","exceptionMessage":"An error occurred while processing this request.","exceptionType":"Microsoft.OData.Client.DataServiceQueryException","stackTrace":" at Microsoft.OData.Client.QueryResult.EndExecuteQuery[TElement](Object source, String method, IAsyncResult asyncResult)\r\n at Microsoft.OData.Client.DataServiceRequest.EndExecute[TElement](Object source, DataServiceContext context, String method, IAsyncResult asyncResult)\r\n at Microsoft.OData.Client.DataServiceQuery`1.EndExecute(IAsyncResult asyncResult)\r\n at Microsoft.Office365.OutlookServices.Extensions.DataServiceContextWrapper.<>c__DisplayClass12`2.<ExecuteSingleAsync>b__10(IAsyncResult i)\r\n at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Office365.OutlookServices.Extensions.DataServiceContextWrapper.<ExecuteSingleAsync>d__14`2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at Microsoft.Office365.OutlookServices.MessageFetcher.<ExecuteAsync>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n at [[my code]]\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.GetResult()\r\n at [[My code]]","innerException":{"message":"An error has occurred.","exceptionMessage":"NotFound","exceptionType":"Microsoft.OData.Client.DataServiceClientException","stackTrace":" at Microsoft.OData.Client.BaseAsyncResult.EndExecute[T](Object source, String method, IAsyncResult asyncResult)\r\n at Microsoft.OData.Client.QueryResult.EndExecuteQuery[TElement](Object source, String method, IAsyncResult asyncResult)"}} 

异常详细信息从VS异常asisstant复制:

Microsoft.OData.Client.DataServiceQueryException was caught 
    HResult=-2146233079 
    Message=An error occurred while processing this request. 
    Source=Microsoft.OData.Client 
    StackTrace: 
     at Microsoft.OData.Client.QueryResult.EndExecuteQuery[TElement](Object source, String method, IAsyncResult asyncResult) 
     at Microsoft.OData.Client.DataServiceRequest.EndExecute[TElement](Object source, DataServiceContext context, String method, IAsyncResult asyncResult) 
     at Microsoft.OData.Client.DataServiceQuery`1.EndExecute(IAsyncResult asyncResult) 
     at Microsoft.Office365.OutlookServices.Extensions.DataServiceContextWrapper.<>c__DisplayClass12`2.<ExecuteSingleAsync>b__10(IAsyncResult i) 
     at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization) 
    --- End of stack trace from previous location where exception was thrown --- 
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
     at Microsoft.Office365.OutlookServices.Extensions.DataServiceContextWrapper.<ExecuteSingleAsync>d__14`2.MoveNext() 
    --- End of stack trace from previous location where exception was thrown --- 
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
     at Microsoft.Office365.OutlookServices.MessageFetcher.<ExecuteAsync>d__0.MoveNext() 
    --- End of stack trace from previous location where exception was thrown --- 
     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 
     at [[My code]] 
    InnerException: Microsoft.OData.Client.DataServiceClientException 
     HResult=-2146233079 
     Message=NotFound 
     Source=Microsoft.OData.Client 
     StatusCode=404 
     StackTrace: 
      at Microsoft.OData.Client.BaseAsyncResult.EndExecute[T](Object source, String method, IAsyncResult asyncResult) 
      at Microsoft.OData.Client.QueryResult.EndExecuteQuery[TElement](Object source, String method, IAsyncResult asyncResult) 
     InnerException: 
ID')

如何获得访问令牌:因为它转化为客户端

 var authContext = new AuthenticationContext(
      GetAuthorityUrl() 
      .ToString() 
     ); 

     var result = await authContext.AcquireTokenAsync(
       GetResourceUrl(), 
       new ClientAssertionCertificate(GetClientId(), AppCertificateHelper.GetCertificate()) 
      ); 

错误信息

响应标题:

[0]: {[request-id, e459455e-8bba-42b9-89cb-f6eb94f67b64]} 
[1]: {[X-CalculatedBETarget, HK2PR02MB0868.apcprd02.prod.outlook.com]} 
[2]: {[X-BackEndHttpStatus, 404]} 
[3]: {[X-DiagInfo, HK2PR02MB0868]} 
[4]: {[X-BEServer, HK2PR02MB0868]} 
[5]: {[X-FEServer, AM3PR07CA0025]} 
[6]: {[Content-Length, 0]} 
[7]: {[Date, Fri, 06 Feb 2015 18:43:12 GMT]} 
[8]: {[Set-Cookie, ClientId=5MTQKAN4QEAXMVUEF5DF0W; expires=Sat, 06-Feb-2016 18:43:06 GMT; path=/; secure; HttpOnly]} 
[9]: {[Server, Microsoft-IIS/8.0]} 
[10]: {[X-Powered-By, ASP.NET]} 
+0

这是相同的问题http://stackoverflow.com/questions/28333362/getting-cannot-resolve-the-odata-request-url-for-specific-users-on-rest-item? – 2015-02-06 15:33:02

+0

不确定。因为当我使用Web API HttpClient类发出普通请求时,我得到了这个错误400,但是这次使用Office 365工具(用于Outlook的Office365 c#客户端库)。为什么它给出不同的状态代码,如果它是同样的问题?但是,是的,这是一个失败的尝试解决这个问题。 – 2015-02-06 15:42:54

+0

我不确定这是不是相同的错误,这就是为什么我问:)。你能发布完整的错误响应吗? – 2015-02-06 15:48:01

回答

0

答案很简单,就像吃一个苹果一样...... 这个错误的原因是端点URL中的拼写错误。 坏URI:

https://outlook.office365.com/api/Users('mailbox%40domain.onmicrosoft.com')/Messages('long message id') 

更好的URI:

https://outlook.office365.com/api/v1.0/Users( '邮箱%40domain.onmicrosoft.com')/消息( '长消息ID')

差异 - API版本指定。

https://msdn.microsoft.com/office/office365/APi/mail-rest-operations#MessageoperationsUpdatemessages