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);
Registered an application in Azure AD with below permissions:
Generated Client Secret for registered application
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
App registration of type Accounts in this organizational directory only
with below permissions:
Generate token using below scope:
string[] scopes = new string[] { $"https://graph.microsoft.com/.default" };
Error:
"/me request is only valid with delegated authentication flow."
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.
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:
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);
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
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
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).
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: