Predix Analytics service with Angular Http request

218 Views Asked by At

We are trying to use a analytics service in predix. We have successfully generated the Access token and are able to get the time series data through time series service.

But when we are trying to use the analytics service with my angular code http request. Its throwing the 401 authorization error > Full crendential are need to access. But if i request this using Python or Postman service is working fine and we are getting required output.

Please if anyone has idea how to hit the predix analytics service from angular.

Below is the code :

  $http({
method: 'POST',
   url: 'https://predix-analytics-catalog-release.run.aws-usw02-pr.ice.predix.io/api/v1/catalog/analytics/315f7994-873d-4490-bf13-b21edd25e6bd/deployment',
   headers: {
     'authorization': ctrl.token,
     'predix-zone-id': '7b836258-b05d-4abb-9b63-b6705a4c3946',
     'content-type': 'application/json'
   },
   data: {
     "time_series": [
       112,
       118,
       132,
       129,
       121,
       135,
       148,
       148,
       136,
       119,
       104,
       118
     ],
     "params": {
       "num_forecast": 6,
       "method": "SE",
       "period": 12
     }
   }
 }).then(function successCallback(response) {
   console.log("Excecuted")
 }, function errorCallback(response) {
   if (response.status = 401) { // If you have set 401
     console.log("ohohoh hell yaaa");
   }
 });
3

There are 3 best solutions below

0
On

Please add Bearer prefix in authorization header and if still error exists then please try adding origin header also.

headers:

{
 'authorization': 'Bearer '+ctrl.token,
 'predix-zone-id': '7b836258-b05d-4abb-9b63-b6705a4c3946',
 'content-type': 'application/json',
 'origin':'localhost:<your_port>'
}
0
On

Here are a few suggestions:

  1. In your code above, make sure that ctrl.token includes the word "Bearer", like "Bearer 23kjfdu8rj34kfe08fu"
  2. Make sure the token includes authorities to access the instance of Analytics catalog.
  3. Try the Predix Toolkit to help debug your problem: https://predix-toolkit.run.aws-usw02-pr.ice.predix.io/ There you can decode the token, configure UAA, and make API calls to Predix services.
  4. Consider making AJAX requests from Angular back to your web server, and using that server to proxy requests to back end services. This avoids CORS issues. Here's an example: https://github.com/PredixDev/predix-seed/tree/develop/server
0
On

Here possible error is Bearer not passed, Bearer needs to be applied before access token in Authorization headers

Authorization Bearer 17MHIFGLIE1d43bPxcnxEmRI5tyT

so the above code would be

'authorization': 'bearer '+ctrl.token,