Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KaiWalter opened this issue Dec 4, 2020 · 4 comments · Fixed by #486
Closed

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

KaiWalter opened this issue Dec 4, 2020 · 4 comments · Fixed by #486

Comments

@KaiWalter
Copy link

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.
@ghost ghost added the ToTriage label Dec 4, 2020
@KaiWalter
Copy link
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

@georgend
Copy link
Contributor

georgend commented Dec 10, 2020

@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" />

@georgend georgend reopened this Dec 10, 2020
@KaiWalter
Copy link
Author

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

@KaiWalter
Copy link
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
Projects
None yet
3 participants