SOLVED

Graph API request doesn't even send out.

%3CLINGO-SUB%20id%3D%22lingo-sub-2203363%22%20slang%3D%22en-US%22%3EGraph%20API%20request%20doesn't%20even%20send%20out.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2203363%22%20slang%3D%22en-US%22%3E%3CP%3ESo%20I%20am%20using%20the%20latest%20Microsoft.Graph%20NuGet%20package%20in%20a%20VS%202019%20project.%20Looking%20to%20create%20a%20new%20calendar%20event.%20The%20API%20request%20doesn't%20even%20send%20out%2C%20although%20if%20I%20place%20the%20same%20JSON%20request%20body%20in%20the%20Graph%20Explorer%20web%20page%20it%20returns%20a%20successful%20response.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20a%20snippet%20of%20my%20method.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-csharp%22%3E%3CCODE%3Evar%20clientId%20%3D%20%22...%22%3B%0Avar%20tenantId%20%3D%20%22...%22%3B%0Avar%20clientSecret%20%3D%20%22...%22%3B%0Avar%20newEvent%20%3D%20new%20Event()%3B%0A%0AIConfidentialClientApplication%20confidentialClientApplication%20%3D%20ConfidentialClientApplicationBuilder%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.Create(clientId)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.WithTenantId(tenantId)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.WithClientSecret(clientSecret)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.Build()%3B%0A%0AClientCredentialProvider%20authenticationProvider%20%3D%20new%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20ClientCredentialProvider(confidentialClientApplication)%3B%0AHttpClient%20httpClient%20%3D%20GraphClientFactory.Create(authenticationProvider)%3B%0A%0A%2F%2F%20Build%20my%20newEvent%20here...%0A%0A%2F%2F%20This%20requestBody%20is%20identical%20to%20the%20one%20that%20works%20in%20the%20online%20Graph%20Explorer.%0Avar%20requestBody%20%3D%20JsonConvert.SerializeObject(newEvent)%3B%0AHttpRequestMessage%20requestMessage%20%3D%20new%20HttpRequestMessage(HttpMethod.Post%2C%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20string.Format(%22users%2F%7B0%7D%2Fcalendar%2Fevents%22%2C%20upn))%3B%0Avar%20content%20%3D%20new%20System.Net.Http.StringContent(requestBody%2C%20Encoding.UTF8%2C%20%22application%2Fjson%22)%3B%0A%0ArequestMessage.Content%20%3D%20content%3B%0AConsole.WriteLine(requestBody)%3B%0A%0A%2F%2F%20This%20is%20where%20the%20program%20doesn't%20throw%20an%20exception%2C%20but%20doesn't%20perform%20the%20API%20call%20either.%20%0AHttpResponseMessage%20response%20%3D%20await%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20httpClient.SendAsync(requestMessage).ConfigureAwait(false)%3B%0Avar%20jsonResponse%20%3D%20await%20response.Content.ReadAsStringAsync().ConfigureAwait(false)%3B%0Avar%20jObject%20%3D%20JObject.Parse(jsonResponse)%3B%0Avar%20id%20%3D%20(string)jObject%5B%22id%22%5D%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20suggestions%20on%20what%20I%20can%20try%3F%20I%20can%20see%20that%20the%20URI%20is%20calling%20the%20v1.0%2C%20which%20worked%20for%20similar%20methods%20I%20have%20in%20this%20project%20that%20I%20used%20maybe%201-2%20years%20ago.%20Spinning%20my%20wheels%20for%20sure!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2203448%22%20slang%3D%22en-US%22%3ERe%3A%20Graph%20API%20request%20doesn't%20even%20send%20out.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2203448%22%20slang%3D%22en-US%22%3EIf%20there%20was%20some%20way%20I%20could%20debug%20as%20to%20_why_%20the%20HttpResponseMessage%20is%20being%20skipped%20over%20it%20would%20help.%20When%20I%20place%20a%20breakpoint%20there%20the%20program%20just%20advances%20on%20and%20doesn't%20fire%20off%20the%20API%20request.%20I%20checked%20my%20Azure%20app%20registration%20and%20it%20still%20has%20permissions%20for%20the%20entire%20tenant%20organization%20to%20read%2C%20write%2C%20etc.%20And%20I%20re-fired%20off%20the%20admin%20consent%20that%20came%20back%20admin_consent%3DTrue%20in%20the%20redirect%20URI.%3CBR%20%2F%3E%3CBR%20%2F%3EThere%20are%20several%20other%20Microsoft%20Graph%20API%20methods%20in%20this%20project%20that%20I%20know%20worked%201-2%20years%20ago.%3C%2FLINGO-BODY%3E
Occasional Contributor

So I am using the latest Microsoft.Graph NuGet package in a VS 2019 project. Looking to create a new calendar event. The API request doesn't even send out, although if I place the same JSON request body in the Graph Explorer web page it returns a successful response.

 

Here is a snippet of my method.

 

var clientId = "...";
var tenantId = "...";
var clientSecret = "...";
var newEvent = new Event();

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create(clientId)
                .WithTenantId(tenantId)
                .WithClientSecret(clientSecret)
                .Build();

ClientCredentialProvider authenticationProvider = new 
                ClientCredentialProvider(confidentialClientApplication);
HttpClient httpClient = GraphClientFactory.Create(authenticationProvider);

// Build my newEvent here...

// This requestBody is identical to the one that works in the online Graph Explorer.
var requestBody = JsonConvert.SerializeObject(newEvent);
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, 
                string.Format("users/{0}/calendar/events", upn));
var content = new System.Net.Http.StringContent(requestBody, Encoding.UTF8, "application/json");

requestMessage.Content = content;
Console.WriteLine(requestBody);

// This is where the program doesn't throw an exception, but doesn't perform the API call either. 
HttpResponseMessage response = await 
                httpClient.SendAsync(requestMessage).ConfigureAwait(false);
var jsonResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var jObject = JObject.Parse(jsonResponse);
var id = (string)jObject["id"];

 

Any suggestions on what I can try? I can see that the URI is calling the v1.0, which worked for similar methods I have in this project that I used maybe 1-2 years ago. Spinning my wheels for sure!

 

3 Replies
If there was some way I could debug as to _why_ the HttpResponseMessage is being skipped over it would help. When I place a breakpoint there the program just advances on and doesn't fire off the API request. I checked my Azure app registration and it still has permissions for the entire tenant organization to read, write, etc. And I re-fired off the admin consent that came back admin_consent=True in the redirect URI.

There are several other Microsoft Graph API methods in this project that I know worked 1-2 years ago.
I can tell this the ConfidentialClientApplicationBuilder code section is where it's failing. Based on that class instance I cannot hook into anything, whether through the GraphServiceClient or the GraphClientFactory.Create() that spins up an HttpClient. I have verified that my clientId, tenantId, and clientSecret are valid in the Azure app registration page. And I've also tried to acquire a token, as part of the process (which I previously didn't have to do when my project was working).

string[] scopes = new string[] { "https://graph.microsoft.com/.default" };

AuthenticationResult result = null;
try
{
result = await confidentialClientApplication.AcquireTokenForClient(scopes)
.ExecuteAsync();
}
catch (MsalServiceException ex)
{
// Case when ex.Message contains:
// AADSTS70011 Invalid scope. The scope has to be of the form "https://resourceUrl/.default"
// Mitigation: change the scope to be as expected
}

best response confirmed by gregarican (Occasional Contributor)
Solution
Disregard. This all was being called as a static method from my main program. I forgot to grab the created calendar event ID with the .Result suffix. Since it's awaiting. Duh.