I am querying Azure Active Directory using Microsoft.Graph. My method (shown below) works in a console app i created as a POC. When i try and use the method in an azure function it returns a HTTP 500 error, but does not give me any information to track down the issue.
The issue occurs on the below line:
IGraphServiceUsersCollectionPage usersPage = await graphClient
.Users
.Request()
.Filter($"Department eq 'Information Technology'")
.Select("Department,DisplayName,Mail,Id")
.GetAsync();
The program hangs there for about 5 seconds and then postman shows a 500 internal error. Appreciate advice on how to troubleshoot or resolve this.
Full Method:
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
string tenantId = "xxxx-xxxx-xxxx-xxxx";
string clientId = "xxxx-xxxx-xxxx-xxxx";
string clientSecretValue = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
List<User> users = new List<User>();
var scopes = new[] { "https://graph.microsoft.com/.default" };
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecretValue);
var graphClient = new GraphServiceClient(clientSecretCredential, scopes);
IGraphServiceUsersCollectionPage usersPage = await graphClient
.Users
.Request()
.Filter($"Department eq 'Information Technology'")
.Select("Department,DisplayName,Mail,Id")
.GetAsync();
users.AddRange(usersPage.CurrentPage);
return new OkObjectResult(users);
}
** I'm aware hard coding secrets is terrible practice, this was just a proof of concept before investing too much time.
UPDATE: The function is long running (taking about 3 minutes to run completely). It seems it is running correctly if i get its URL and execute it in a browser, it just does not run correctly in the "Code & Test feature" on the azure portal, functions blade
