Powershell/REST 401 Error

Had this working earlier. Did not change anything that I can remember. All the sudden I started getting the following error:

ERROR Invoke-RestMethod : 



401 - Unauthorized: Access is denied due to invalid credentials.





Server Error

 
  401 - Unauthorized: Access is denied due to invalid credentials.
  You do not have permission to view this directory or page using the credentials that you supplied.

The important code snippets look like this:

$EpicorUser = 'epicor' #changed for this post
$EpicorPass = 'epicor' #changed for this post
$EpicorSecurePass = ConvertTo-SecureString $EpicorPass -AsPlainText -Force 
$cred = New-Object System.Management.Automation.PSCredential ($EpicorUser, $EpicorSecurePass)
$uri = "https://servername/epicorerp/api/v1/Ice.BO.DynamicQuerySvc/ExecuteByID" 
$json = @"
        {
              "queryID": "REST-QUERY",
              "executionParams": {
                "ExecutionFilter": [],
                "ExecutionParameter": [
                  {
                    "ParameterID": "CodeID",
                    "ParameterValue": $Code,
                    "ValueType": "string",
                    "IsEmpty": false,
                    "SysRowID": "00000000-0000-0000-0000-000000000000",
                    "RowMod": "string"
                  }
                ],
                "ExecutionSetting": [],
                "ExecutionValueSetItems": [],
                "ExtensionTables": []
              }
        }
"@
        $headers = New-Object System.Collections.Generic.Dictionary"[String,String]"
        $headers.Add("accept", "application/json")
        $headers.Add("Content-Type", "application/json")
        $headers.Add("Authorization","Bearer Token Here")
        $data = Invoke-RestMethod $uri -Method POST -Credential $cred -H $headers -body $json

I got most of my code from this post:

and to my knowledge I have the same code now as what fixed this prior issue.

Any thoughts?

1 Like

Where is the actual auth token? Because it’s not “Here”…

It is there I just replaced it so the post was a little more readable.

$headers.Add("Authorization","Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOiIxNjkyMDMxODM1IiwiaWF0IjoiMTY5MjAyODIzNSIsImlzcyI6ImVwaWNvciIsImF1ZCI6ImVwaWNvciIsInVzZXJuYW1lIjoiVVMxODc4MDEifQ.H_cIhJxXmQkerc0tMCsfCZ1NFlkFqtybnL9Q5KaUdgQ")

anything in the server event viewer?

Nope, not seeing anything.

Are you sure the token is still good? They do expire eventually, you are supposed to get a new one each session… Also, why are you also passing creds? All you should need is the Authorization header, and the X-API-Key header… (Edit: Actually since you are using v1 you don’t need the API key…)

I’ve been told that with REST v1 API Key is not needed. I got this code from the RESThelp page Epicor provides, and took the bearer token from there.

You are supposed to obtain a token by POSTing to https://servername/epicorerp/TokenResource.svc/ with basic auth. From there you use that token in your subsequent queries.

So I would say remove the -Credential $cred switch from your Invoke-RestMethod, and use a fresh token, and you should be golden…

1 Like

I am very new to REST so let me confirm what you are saying.

$tokenuri = "https://servername/epicorerp/TokenResource.svc/"
$token = Invoke-RestMethod $tokenuri -Method POST -Credential $cred

Something like this?

Yup, also, add header Accept: application/json otherwise you’ll get XML back…

1 Like
ERROR Invoke-RestMethod : 


  
    Request Error
    BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { 
color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: 
#e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; 
background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}
  
  
    
      Request Error
      The server encountered an error processing the request. Please see the service help page for constructing valid requests to the service. The exception message is 'The caller was not authenticated by the service.'. See server logs for more details. The exception stack trace is: 
         at System.ServiceModel.Dispatcher.AuthenticationBehavior.Authenticate(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
$headers = New-Object System.Collections.Generic.Dictionary"[String,String]"
$headers.Add("accept", "application/json")
$tokenuri = "https://grecorappd01.corp.ad.ufpi.com/epicorerp/TokenResource.svc/"
$token = Invoke-RestMethod $tokenuri -Method POST -Credential $cred -H $headers

Not a big fan of the black box $cred thingie (or of powershell for that matter)… Try just adding the header Authorization: Basic (user:pass encoded to Base64), no -Credential switch…

$headers = New-Object System.Collections.Generic.Dictionary"[String,String]"
$headers.Add("accept", "application/json")
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization","Bearer $($token.AccessToken)")
$data = Invoke-RestMethod $uri -Method POST -H $headers -body $json
ERROR Invoke-RestMethod : {"HttpStatus":400,"ReasonPhrase":"REST API Exception","ErrorMessage":"Parameter queryID is not found in the input object","ErrorType":"Epicor.RESTApi.ErrorHandling.ApiException","CorrelationId":"dab3ad28-fae5-43aa-9823-35af72a88f60"}

Progress! I navigated a few other errors and have landed here now. I know I have seen this before but forget what caused it. The token is creating successfully I have it printing to my screen before this code is run.