2016-07-06 10 views
2

私は現在、ASP .NET MVCでMicrosoft Graph APIを使用してユーザーのOutlook予定表にイベントを作成しようとしています。残念ながら、イベントを作成するためのドキュメントにはサンプルが含まれていません。私は電子メールを送信するためのサンプルを見つけて修正しようとしました(ここではhttps://github.com/microsoftgraph/aspnet-connect-rest-sample)。私はカレンダーに最初のイベントを完全に空白で送ることができました。イベントオブジェクト自体を送信しようとすると、BAD REQUESTのステータスレスポンスが返されます。誰かが助けることができれば、それは非常に高く評価されるだろう。Microsoft Graphのイベントオブジェクトを正しく表現するにはどうすればよいですか?

答えて

2

Eventオブジェクトの構築方法については、公式のMicrosoft Graph SDKがどのようにこのオブジェクトを構成しているかをご覧ください。最新のソースhereをGitHubでご覧ください。

SDKを使用せずにこのREST呼び出しを行う例については、UserSnippets#CreateEventAsync()を参照することができます。下の抜粋が貼り付けられています。以下の例では、シリアライゼーションにネイティブオブジェクトを使用していませんが、それがうまくいくかどうかの本質を伝えることができます。送信されたJSONがどのように見えるかの例については

HttpClient client = new HttpClient(); 
var token = await AuthenticationHelper.GetTokenHelperAsync(); 
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token); 

// Endpoint for the current user's events 
Uri eventsEndpoint = new Uri(serviceEndpoint + "me/events"); 

// Build contents of post body and convert to StringContent object. 
// Using line breaks for readability. 

// Specifying the round-trip format specifier ("o") to the DateTimeOffset.ToString() method 
// so that the datetime string can be converted into an Edm.DateTimeOffset object: 
// https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Roundtrip 

string postBody = "{'Subject':'Weekly Sync'," + "'Location':{'DisplayName':'Water Cooler'}," + "'Attendees':[{'Type':'Required','EmailAddress': {'Address':'[email protected]'} }]," + "'Start': {'DateTime': '" + new DateTime(2014, 12, 1, 9, 30, 0).ToString("o") + "', 'TimeZone':'UTC'}," + "'End': {'DateTime': '" + new DateTime(2014, 12, 1, 10, 0, 0).ToString("o") + "', 'TimeZone':'UTC'}," + "'Body':{'Content': 'Status updates, blocking issues, and next steps.', 'ContentType':'Text'}}"; 

var createBody = new StringContent(postBody, System.Text.Encoding.UTF8, "application/json"); 

HttpResponseMessage response = await client.PostAsync(eventsEndpoint, createBody); 

if (response.IsSuccessStatusCode) { 
string responseContent = await response.Content.ReadAsStringAsync(); 
jResult = JObject.Parse(responseContent); 
createdEventId = (string) jResult["id"]; 
Debug.WriteLine("Created event: " + createdEventId); 
} else { 
// some appropriate error handling here 
} 

{ 
    "subject": "Weekly Sync", 
    "location": { 
     "displayName": "Water Cooler" 
    }, 
    "attendees": [ 
     { 
      "type": "Required", 
      "emailAddress": { 
       "address": "[email protected]" 
      } 
     } 
    ], 
    "start": { 
     "dateTime": "2016-02-02T17:45:00.0000000", 
     "timeZone": "UTC" 
    }, 
    "end": { 
     "dateTime": "2016-02-02T18:00:00.0000000", 
     "timeZone": "UTC" 
    }, 
    "body": { 
     "content": "Status updates, blocking issues,and nextsteps.", 
     "contentType": "Text" 
    } 
} 

追加ヘルプ:あなたのブラウザでリクエストをテストするために役立つGraph Explorer Sandbox利用できるあります

関連する問題