Skip to content

extend Invoke-MgGraphRequest to handle PUT body / Unauthorized during build #483

Closed

Description

@KaiWalter

I need to PUT a body with this operation https://graph.microsoft.com/beta/identityGovernance/entitlementManagement/accessPackageAssignmentPolicies/{id} and get a 400 Bad Request although the same body works on the respective Graph API operation.

If I interpret InvokeMgGraphRequest.cs correctly, the body is just not added to the request stream for a PUT method operation.

I wanted to extend the CmdLet myself but already struggle with
.\tools\GenerateAuthenticationModule.ps1 -Build

which gives me an authentication error

WARNING: System.Collections.Hashtable
WARNING: 1.2.0
WARNING: Valid
WARNING: 1.2.0
WARNING: 1.2.0
Updated the .csproj.
Building 'Authentication' module...
Creating isolated process...
Cleaning build folders...
Compiling module...
C:\Program Files\dotnet\sdk\3.1.302\NuGet.targets(128,5): error : Failed to retrieve information about 'NETStandard.Library' from remote source 'https://pkgs.dev.azure.com/microsoftgraph/0985d294-5762-4bc2-a565-161ef349ca3e/_packaging/b395d603-5c77-4af9-a495-1e3cdbb49e1e/nuget/v2/FindPackagesById()?id='NETStandard.Library'&semVerLevel=2.0.0'. [C:\Users\kai\src\msgraph-sdk-powershell\src\Authentication\Authentication\Microsoft.Graph.Authentication.csproj]
C:\Program Files\dotnet\sdk\3.1.302\NuGet.targets(128,5): error :   Response status code does not indicate success: 401 (Unauthorized). [C:\Users\kai\src\msgraph-sdk-powershell\src\Authentication\Authentication\Microsoft.Graph.Authentication.csproj]
Write-Error: C:\Users\kai\src\msgraph-sdk-powershell\src\Authentication\Authentication\build-module.ps1:47
Line |
  47 |    Write-Error 'Compilation failed.'
     |    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Compilation failed.

Activity

  1. KaiWalter commented on Dec 4, 2020

    @KaiWalter
    Author

    currently I use this workaround

            $client = [Microsoft.Graph.PowerShell.Authentication.Helpers.HttpHelpers]::GetGraphHttpClient()
            $stringcontent = New-Object System.Net.Http.StringContent(($body | ConvertTo-Json -Depth 15), [System.Text.Encoding]::UTF8, "application/json")
            return $client.PutAsync($uri, $stringcontent).GetAwaiter().GetResult()
    

    instead of

    return Invoke-MgGraphRequest -Uri $uri -Method Put -Body $body
    
  2. georgend commented on Dec 10, 2020

    @georgend
    Contributor

    Kai Walter (@KaiWalter) We have fixed the issue you noted. The bad request was caused by Invoke-GraphRequest sending the wrong Content-Type header text/plain instead of application/json for PUT requests when -ContentType parameter is not set.
    A fix will be released with the next version.

    In the mean while a workaround would be to specify the -ContentType explicitly.
    Invoke-MgGraphRequest -Uri $uri -Method Put -Body $body -ContentType application/json

    To fix the unauthorized issue during build, remove the following lines from Nuget.config. They point to our internal build feed, we will correct this as well in the next build.

    <clear />
    <add key="PowerShellSDK_BuildFeed" value="https://pkgs.dev.azure.com/microsoftgraph/0985d294-5762-4bc2-a565-161ef349ca3e/_packaging/PowerShellSDK_BuildFeed/nuget/v3/index.json" />
    <add key="LocalNugetFeed" value="https://pkgs.dev.azure.com/microsoftgraph/0985d294-5762-4bc2-a565-161ef349ca3e/_packaging/b395d603-5c77-4af9-a495-1e3cdbb49e1e/nuget/v2" />

  3. KaiWalter commented on Dec 11, 2020

    @KaiWalter
    Author

    thanks finsharp - adding -ContentType "application/json" worked - I will try nuget feed next

  4. KaiWalter commented on Dec 12, 2020

    @KaiWalter
    Author

    removing lines from Nuget.config worked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions