Microsoft Graph HTTP REST API, GET deviceAppManagement/mobileApps

George Tsirkas 0 Reputation points
2023-05-10T09:18:51.1766667+00:00

Hello i have created powershell script which get a list of mobileApps. I follow this doc. https://learn.microsoft.com/en-us/graph/api/intune-apps-win32lobapp-list?view=graph-rest-1.0

# Connect to Microsoft Graph API

$appId = ""

$tenantId = ""

$secret = ""

$scopes = "https://graph.microsoft.com/.default"

$token = get-msaltoken -tenantid $tenantId -clientid $appId -clientsecret ( $secret |ConvertTo-SecureString -AsPlainText -Force) -Scopes $scopes

Connect-MgGraph -AccessToken $token.accesstoken

$Headers = @{

        "Authorization" = "Bearer $($token.AccessToken)"

        "Content-type"  = "application/json"

        "ExpiresOn" = $token.ExpiresOn

    }

$url = "https://graph.microsoft.com/v1/deviceAppManagement/mobileApps"

$response = Invoke-RestMethod -Method GET -Uri $url -Headers $Headers

appId, tenantId and secret are corrent. Also i have given the correct permissions (DeviceManagementApps.Read.All, DeviceManagementApps.ReadWrite.All)

I get the following error.

Welcome To Microsoft Graph!
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.
At line:23 char:13
+ $response = Invoke-RestMethod -Method GET -Uri $url -Headers $Headers
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Has anyone the same error?

Windows for business Windows Server User experience PowerShell
Microsoft Security Intune Other
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. TH-4749-MSFT 3,315 Reputation points
    2023-05-10T15:49:34.3466667+00:00

    Hello George Tsirkas,

    Thanks for reaching out. A (401) Unauthorized error usually occurs if the access token is not valid. Please ensure you are using a valid access token. For more information please refer to article https://learn.microsoft.com/en-us/graph/resolve-auth-errors.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    Thanks.


  2. Crystal-MSFT 53,981 Reputation points Microsoft External Staff
    2023-05-11T02:41:25.82+00:00

    @George Tsirkas, Thanks for posting in Q&A. From your description, I know you want to get all the apps in Intune but it has issue with token.

    I have custom a PowerShell script to get all the apps. You can change the name of LISTALLAPPS.txt to LISTALLAPPS.ps1. And run the script as administrator, enter the global administrator account to sign in to get the info.

    User's image

    Hope the above information can help.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.