homotechsual / NinjaOne

MIT License
85 stars 25 forks source link

Build Failed #13

Closed jpleasants closed 2 years ago

jpleasants commented 2 years ago

Downloaded and installed the latest .NET SDK (v6.0.302) in order to establish connection to the NinjaOne API. Input my information and executed "Connect-NinjaOne". Page came up in my browser and I gave my authorization. Site redirected to, "localhost refused to connect". Went back to powershell console and received "the build failed The build failed. Fix the build errors and run again.". Ran netstat to confirm nothing was listening on my specified port.

Uninstalled .NET SDK 6.0 and installed the latest .NET SDK 5.0 (v5.0.408). Restarted powershell console and browser and attempted same command. This time, received my refresh token. Able to confirm connection to API through several of the Get-* cmdlets

homotechsual commented 2 years ago

Looking into this!

homotechsual commented 2 years ago

Can confirm this is working with .NET 6.0.400 currently. It should however be compatible with all supported .NET versions.

notadevcollins commented 2 years ago

just a note, I am currently experiencing the same situation as jpleasants using 6.0.400, tried with 7 and got the same luck of build failed, will try on a fresh windows 10 install tomorrow if there is time and report back.

homotechsual commented 2 years ago

Can you show me the output from dotnet --version

notadevcollins commented 2 years ago

dotnet --version

6.0.400

PowerShell 7.2.6

The build failed. Fix the build errors and run again.

Invoke-WebRequest: C:\Users\User0\Documents\PowerShell\Modules\NinjaOne\1.2.3\Public\Connect-NinjaOne.ps1:179 Line | 179 | $TokenResult = Invoke-WebRequest @TokenRequestParams | ~~~~~~~~~ | {"error":"invalid_token"}

notadevcollins commented 2 years ago

using dot net 5 was the only way I found to get it to work for me

homotechsual commented 2 years ago

Okay, well further and further down the rabbit hole I went before I got very annoyed with OAuthListener and so I "fucked it right off".

The module now uses .NET classes to create an in-process HTTP listener for the OAUTH code.

Relevant part of Connect-NinjaOne follows:

        try {
            # Get our authorisation code.
            Write-Verbose 'Opening browser to authenticate.'
            Write-Debug "Authentication URL: $($AuthRequestURI.ToString())"
            $HTTP = [System.Net.HttpListener]::new()
            $HTTP.Prefixes.Add("http://localhost:$Port/")
            $HTTP.Start()
            Start-Process $AuthRequestURI.ToString() 
            while ($HTTP.IsListening) {
                $Context = $HTTP.GetContext()
                Write-Debug $Context.Request.QueryString
                if ($Context.Request.QueryString -and $Context.Request.QueryString['Code']) {
                    $Script:NRAPIAuthenticationInformation.Code = $Context.Request.QueryString['Code']
                    Write-Debug "Authorisation code received: $($Script:NRAPIAuthenticationInformation.Code)"
                    if ($null -ne $Script:NRAPIAuthenticationInformation.Code) {
                        $GotAuthorisationCode = $True
                    }
                    [string]$HTML = '<h1>NinjaOne PowerShell Module</h1><br /><p>An authorisation code has been received. You can close this window now. The HTTP listener will stop in 5 seconds.</p>'
                    $Response = [System.Text.Encoding]::UTF8.GetBytes($HTML)
                    $Context.Response.ContentLength64 = $Response.Length
                    $Context.Response.OutputStream.Write($Response, 0, $Response.Length)
                    $Context.Response.OutputStream.Close()
                    Start-Sleep -Seconds 5
                    $HTTP.Stop()
                }
            }
        } catch {
            New-NinjaOneError -ErrorRecord $_
        }
homotechsual commented 2 years ago

Can you both (@jpleasants @notadevcollins) test 1.3.0 for me?

notadevcollins commented 2 years ago

Well done, it now works for me with dot net 6.0.400 thank you for your work!

homotechsual commented 2 years ago

It no longer requires dotnet at all beyond the requirements of PowerShell 7

notadevcollins commented 2 years ago

oh! my mistake, that's great to hear, one less thing I need installed