I have an API where I’m using MS Graph to retrieve details for documents stored in SharePoint using the document’s ID like this:
[HttpGet]
public async Task<IActionResult> ById(string id)
{
try
{
DriveItem result = await _graphServiceClient.Sites["XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"].Lists["Documents"].Items[id].DriveItem.Request().WithAppOnly().GetAsync();
if (result != null)
{
return Ok(result);
}
else
{
return NotFound();
}
}
catch (Exception ex)
{
return NotFound(ex.Message + "... " + ex.StackTrace); //This is firing in PROD
}
}
This works perfectly when I run from localhost. When I publish the application to a production server, however, an exception is thrown and the catch
above fires, showing a “generalException” from Graph and the following stacktrace:
Code: generalException
Message: An error occurred sending the request.
... at Microsoft.Graph.HttpProvider.SendRequestAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.HttpProvider.SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken)
at Microsoft.Graph.BaseRequest.SendRequestAsync(Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
at Microsoft.Graph.BaseRequest.SendAsync[T](Object serializableObject, CancellationToken cancellationToken, HttpCompletionOption completionOption)
at Microsoft.Graph.DriveItemRequest.GetAsync(CancellationToken cancellationToken)
at MemberDocAPI.Controllers.GetDocumentController.ById(String id) in C:codeMyAPIControllersGetDocumentController.cs:line 34
Given how vague the error is, I have no idea what’s causing the problem. Also, I’m not sure if it’s relevant or not but the app is registered with application level permissions instead of delegated.
What could be causing my issue? Thanks!
2
I modified my catch
to the following:
catch (Exception ex)
{
return NotFound(ex.InnerException);
}
And it told me:
One client credential type required either: ClientSecret, Certificate, ClientAssertion or AppTokenProvider must be defined when creating a Confidential Client. Only specify one.
So I created a ClientSecret from the Azure Portal and added its value in my appsettings.json:
This solved the problem for me.