For the context, I'm building an app that will read and update the Custom Security Attributes of Azure AD users. I'm using the beta version.
Here is my code:
# Connect to the client
Function Invoke-Connect-MSGraph($ClientId, $TenantId, $ClientSecret) {
    $body = @{
        'grant_type'    = 'client_credentials'
        'client_id'     = $ClientId
        'client_secret' = $ClientSecret
        # Scope has to be .default when using client cred flow: https://stackoverflow.com/a/51789899/12739456
        'scope'         = 'https://graph.microsoft.com/.default'
    }
    
    $Params = @{
        'Uri'         = "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token"
        'Method'      = 'Post'
        'ContentType' = 'application/x-www-form-urlencoded'
        'Body'        = $body
    }
    
    try {
        $TokenResponse = Invoke-RestMethod @Params
        $AccessToken = $TokenResponse.access_token 
        Connect-MgGraph -AccessToken $AccessToken 
        Select-MgProfile -Name "beta"
        Write-Host "Connected to MS Graph"
    }
    catch [Exception] {
        Write-Error "Error Connecting. Error was: $_.Exception.Message"
        exit
    }
}
# Get User
$EmployeeEmail = "janedoe@xyz.com"
$User = Get-MgUser -Filter "proxyAddresses/any(x:x eq 'smtp:$($EmployeeEmail)')"
# Get Custom Security Attributes 
$Attr = Get-MgUser -UserId $User.Id -Property "customSecurityAttributes"
$AzureCustomSecurityAttributes = Convertfrom-json ($Attr).ToJson()
The error I'm getting: Cannot find an overload for "ToJson" and the argument count: "0". It seems that $Attr always returns null.
But I'm able to get other user attributes.
The app has the correct permission: CustomSecAttributeAssignment.ReadWrite.All and User.Read.All
The Admin role I'm using also has the Attribute Assignment Administrator role.
What am I doing wrong here? Thanks in advance for your input!
I tried implementing the same logic using Azure AD PowerShell. It works, but since Azure AD PowerShell is being deprecated, I need to find a solution using MS Graph PowerShell.