Forum Discussion

dandrevbarrio's avatar
dandrevbarrio
Copper Contributor
Jul 25, 2024

PowerShell Script Failing with Auth Header and 500 Internal Server Error for REST API

Hi everyone,

I'm encountering multiple issues with the PowerShell script that interacts with a REST API to execute batch jobs in FDMEE. The script is supposed to send an HTTP request with a Basic Authorization header, but I'm facing the following problems:

  1. "Invalid or Missing Authorization Header" Error: When I visit the API URL directly in the browser, I get:

      {
      "links": [],
      "status": 9,
      "details": "EPMFDM-ERROR: Invalid or Missing Authorization Header in request"
      }

 

  2."Internal Server Error (500)": When running the script, it often goes to the catch block and displays a 500 Internal Server Error. Here's the error message I receive in PowerShell:

        PS>TerminatingError(Invoke-RestMethod): "Request failed."
        Error encountered in PowerShell Script.

 

Here is the script I'm using:
 #HTTP Basic Authorization. Contains encrypted username and password encoded to base64 string.
$headers = @{Authorization = 'Basic encryptedpassword';}

# Set parameter values
$jobName = $args[0]
$uri = http://server.comm.iocs.address.com:0000/aif/rest/V1/jobs

# Monitor status of current batch run
Write-Output "Checking Job Status..."
Start-Sleep -Seconds 5

$restResponse = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers -ContentType "application/json"
$lastJobID = $restResponse.items[0].jobID

$payload = @{
jobType = "BATCH"
jobName = $jobName
} | ConvertTo-Json

# Establish REST connection and execute batch job using REST API
$restResponse = Invoke-RestMethod -Uri $uri -Method Post -Body $payload -Headers $headers -ContentType "application/json"
$uri = $restResponse.links[0].href

# Display initial status of batch
Write-Output "See below status of batch run..."
$restResponse = Invoke-RestMethod -Uri $uri -Method Get -Headers $headers -ContentType "application/json"
$currentJobID = $restResponse.jobID

Write-Output "Last Job ID: $lastJobID"
Write-Output "Current Job ID: $currentJobID"
}
catch {
Write-Output "Error encountered in PowerShell Script.."
Write-Output $_.Exception.Message

if ($_.InvocationInfo) {
Write-Output "Error in script: $($_.InvocationInfo.ScriptName)"
Write-Output "Error on line: $($_.InvocationInfo.ScriptLineNumber)"
Write-Output "Error in command: $($_.InvocationInfo.Line)"
}

if ($_.Exception.Response) {
Write-Output "HTTP Status Code: $($_.Exception.Response.StatusCode.Value__)"
Write-Output "Status Description: $($_.Exception.Response.StatusDescription)"
Write-Output "Response Content: $($_.Exception.Response.Content)"
}

exit 1
}

 

 

Despite my efforts, the request still fails with the "Invalid or Missing Authorization Header" error and occasionally hits a 500 Internal Server Error. Here are the steps I've taken to debug the issues:

  1. Checked Base64 Encoding: Confirmed that the credentials are correctly encoded in Base64.
  2. Verified Header Format: Ensured that the Authorization header is correctly formed and included in the request.
  3. Tested with Postman: Manually tested the API request with Postman using the same Authorization header, and I'm getting the same header authorization error.
  4. Added Detailed Error Logging: Included more detailed error logging in the catch block to capture HTTP status codes and response content.

I'm looking for advice on what might be causing these issues in the PowerShell script and how I can resolve them. Any insights or suggestions would be greatly appreciated!

  • LainRobertson's avatar
    LainRobertson
    Silver Contributor

    dandrevbarrio 

     

    Hi there.

     

    Without knowing anything about the REST API in question, it's nearly impossible to be able to help here.

     

    The only aspect I got curious about is whether the endpoint expects the same authorisation header every call, or whether it's more like the handshake process used on Azure endpoints, where you authenticate first, where if that's successful you get a token in the response, and it's this token you use for subsequent "authentication" in the REST calls.

     

    If the REST API you're using follows a similar design, then this would at least explain why you're getting "does not contain authorisation" errors.

     

    But against that, you said Postman tested worked, and I'm assuming you can see the headers it uses in the first and subsequent calls, so I'm guessing my one thought above may not be the cause.

     

    Again, not having any specifics around this scenario means we're entirely in the dark and reduced to wild guessing.

     

    Cheers,

    Lain

    • dandrevbarrio's avatar
      dandrevbarrio
      Copper Contributor

      Hi Lain,

      the REST API being used is part of Oracle Hyperion Financial Data Quality Management Enterprise Edition (FDMEE). More specifically, Job Management API and Authentication.

       

      Thanks,

      Drev

      • LainRobertson's avatar
        LainRobertson
        Silver Contributor

        dandrevbarrio 

         

        Hi, Drev.

         

        Some of Oracle's documentation is locked behind a paywall while some is publicly accessible. I can only access the latter.

         

        A quick search turned up the first article which points the reader to the second:

         

        1. FDMEE REST APIs (oracle.com)
        2. EPM Cloud REST API basic authentication for Classic and OCI (Gen 2) (oracle.com)

         

        Where at the bottom of the second article is a PowerShell-based example on how to format the Authorization header.

         

        Technically, it's not encrypted, only encoded, so the initial string values you'd be pass into the line via the $userName and $userPassword variables would be the actual plain text values for the relevant username and password.

         

        But if what you're already doing matches the Oracle PowerShell example, then I'm unsure what else it might be, as the first article doesn't cover authentication at all.

         

        The following URL may have more information, but it's locked away behind Oracle's paywall, so I'm simply guessing:

         

         

        Edited to add one final footnote:

        When looking at the Oracle example, ensure you correctly use double-quotes and do not change them for single quotes, as the type of quotes used in PowerShell makes a big difference.

         

        When parsing double-quoted strings, PowerShell will perform any variable substitutions and code executions.

         

        Conversely, single-quoted strings are not parsed at all, where everything enclosed in the string is treated literally, i.e. there are no substitutions.

         

        Cheers,

        Lain

Resources