Search code examples
c#azureazure-functionsmicrosoft-graph-api.net-6.0

List Azure certification from Microsoft Learn profile using Graph API from .net 6 Azure Function


I'm trying to fetch Azure certification list from my Microsoft Learn profile (https://learn.microsoft.com/en-us/users/kuldeepsingh/credentials/certifications) using Graph API.

Azure Functions code to generate token:

        string[] scopes = new string[] { $"{clientId}/.default" };

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

        AuthenticationResult authResult = await confidentialClientApplication.AcquireTokenForClient(scopes).ExecuteAsync();

        string authToken = authResult.AccessToken;
        Console.WriteLine("Access token: {0}", authToken);

Steps

  1. Registered an application in Azure AD with below permissions:

    • MicrosoftGraph - User.Read
    • MicrosoftGraph - Directory.Read.All
  2. Generated Client Secret for registered application

  3. Azure function in .net 6

I'm getting 401 Unauthorized error. Appreciate any help in this context.

Ref: Microsoft documentation at https://learn.microsoft.com/en-us/graph/api/profile-list-certifications?view=graph-rest-beta&tabs=http

Tried option 1

  1. App registration of type Accounts in this organizational directory only with below permissions:

    • MicrosoftGraph - User.Read
    • MicrosoftGraph - User.Read.All
  2. Generate token using below scope:

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

    "/me request is only valid with delegated authentication flow."

UPDATE:

As of now there is no sync between certifications from MS learn profile and /me/profile/certifications. So technically we can't fetch MS Learn certification using Graph APIs.


Solution

  • Note that: For work or school accounts, all delegated permissions are valid; however, not all of them are valid for Microsoft accounts used for personal use. Check this.

    I created an Azure AD Application and granted User.Read API permission:

    enter image description here

    I generated access token with User.Read scope:

    using Microsoft.Identity.Client;
    
    string authority = "https://login.microsoftonline.com/TenantID";
    string clientId = "ClientID"; 
    string clientSecret = "ClientSecret";
    
    string[] scopes = new string[] { "User.Read" };
    
    IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
        .Create(clientId)
        .WithAuthority(authority)
        .WithClientSecret(clientSecret)
        .Build();
    
    var authRequestUrl = confidentialClientApplication.GetAuthorizationRequestUrl(scopes);
    string authorizationCode = "Code";
    AuthenticationResult authResult = await confidentialClientApplication.AcquireTokenByAuthorizationCode(scopes, authorizationCode).ExecuteAsync();
    
    Console.WriteLine("Access token: {0}", authResult.AccessToken);
    

    enter image description here

    By using the above access token when I tried to call the User endpoint, I got the error like below:

    GET https://graph.microsoft.com/beta/users/UserID/profile/certifications
    

    enter image description here

    Hence, to resolve the error instead of calling /users endpoint call /me endpoint to fetch the certificates of signed-in user:

    GET https://graph.microsoft.com/beta/me/profile/certifications
    

    enter image description here

    According to the MsDoc, User.Read.All API permission does not support Microsoft Account and hence you are facing 401 error while access /users endpoint. These permissions are only available for work or school accounts (Azure AD accounts).

    • To access the certifications for a personal Microsoft account, you can use the User.Read permission and call the /me endpoint instead of the /users/id endpoint.

    UPDATE:

    To fetch the data from MS learn profile is not possible is no sync between your certifications in Microsoft Learn and /me/profile/certifications. Please raise a feature request.

    Reference:

    Overview of Microsoft Graph permissions - Microsoft Graph