I am trying to create an LOB [.ipa] app at Intune by calling Graph API through Powershell 2.
I am able to create the application at Intune but when I try to upload an ipa file, I receive:
Bad Request : 400 error.
Below is code snippet:
$tenantId = "XXXXXXXXXXXXXXXXXXX"
$clientId = "XXXXXXXXXXXXXXXXXXXX"
$clientSecret = "XXXXXXXXXXXXXXXXXXX"
$appPath = "./Path to .ipa file" ## Relative Path
$resource = "https://graph.microsoft.com/"
$authUrl = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token"
$authBody = @{
client_id = $clientId
client_secret = $clientSecret
scope = "https://graph.microsoft.com/.default"
grant_type = "client_credentials"
}
$tkResponse = Invoke-RestMethod -Method Post -Uri $authUrl -ContentType "application/x-www-form-urlencoded" -Body $authBody
$origToken = $tkResponse.access_token
$initialheaders = @{
Authorization = "Bearer $origToken"
"Content-Type" = "application/json"
}
$body = @{
"@odata.type" = "#microsoft.graph.iosLobApp"
displayName = "Test"
bundleId = "self.test.sample.app"
buildNumber = "1.0.0"
versionNumber = "1.0.0"
description = "Test description"
publisher = "Test Publisher"
fileName = "Test.ipa"
committedContentVersion = "1"
informationUrl = "https://yourappwebsite.com"
privacyInformationUrl = "https://yourappwebsite.com/privacy"
minimumSupportedOperatingSystem = @{
v8_0 = $true
}
applicableDeviceType = @{
iPad = $true
iPhoneAndIPod = $true
}
}
$mobJson = $body | ConvertTo-Json -Depth 10
$graphApiUrl = "https://graph.microsoft.com/v1.0/deviceAppManagement/mobileApps"
try{
$response = Invoke-RestMethod -Method Post -Uri $graphApiUrl -Headers $initialheaders -Body $mobJson -ErrorAction Stop -verbose
$applicationId = $response.id
} catch {
Write-Host "$($_.Exception.Response.StatusCode)"
}
`
$fileUpUrl = "https://graph.microsoft.com/v1.0/deviceAppManagement/mobileApps/$applicationId/contentVersions/1/files"
$fContent = [System.IO.File]::ReadAllBytes($appPath)
$baseContent = [Convert]::ToBase64String($fContent)
$contheaders = @{
Authorization = "Bearer $origToken"
"Content-Type" = "application/octet-stream"
}
$uploadBody = @{
"@odata.type" = "#microsoft.graph.mobileAppContentFile"
name = (Get-Item $appPath).Name
sizeInBytes = (Get-Item $appPath).Length
versionCode = "1.0.0"
versionName = "Version Name"
contentBytes = $baseContent
} | ConvertTo-Json
try{
**## ERROR COMES FROM BELOW INVOKE METHOD ##**
* $uploadResp = Invoke-RestMethod -Uri $fileUpUrl -Method Post -Headers $contheaders -Body $uploadBody -Verbose*
} catch {
$error = $_.Exception
}
I also tried different ways like sending data as bytes, byte stream, sending body parameter as String but every time I receive “Bad Request : 400” error.
- Application get created perfectly fine
- Bad Request Error comes at second invoke rest method
Below is the error details
2024-12-23T18:16:34.9342611Z Error uploading IPA file: The remote server returned an error: (400) Bad Request.
2024-12-23T18:16:34.9370301Z System.Net.WebException: The remote server returned an error: (400) Bad Request.
2024-12-23T18:16:34.9372124Z at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
2024-12-23T18:16:34.9392176Z at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
2024-12-23T18:16:34.9404004Z Error11: System.Net.WebException: The remote server returned an error: (400) Bad Request.
2024-12-23T18:16:34.9404570Z at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
2024-12-23T18:16:34.9405060Z at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
2024-12-23T18:16:34.9406974Z Error11 Message: System.Net.WebException: The remote server returned an error: (400) Bad Request.
2024-12-23T18:16:34.9407299Z at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetResponse(WebRequest request)
2024-12-23T18:16:34.9408070Z at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord().Message
Amit Saxena is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2