SOLVED

Graph API request doesn't even send out.

Copper 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 (Copper 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.
1 best response

Accepted Solutions
best response confirmed by gregarican (Copper 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.

View solution in original post