You may try to create a PHP script as described below and after that instead calling Magento 2 API directly from the angular app (xyz.test.com), call this PHP script with some parameter. In the PHP script, you can add Access-Control-Allow-Origin to the header.
It is not the best solution but works.
step 1)
Create a PHP file under Magento 2 root
Please change Site Url , API User name and API Password in the script.
File: apiconnect.php
<?php
header("Content-type: application/json; charset=utf-8");
header("Access-Control-Allow-Origin: *")
$magentoSiteUrl = 'http://www.YOUR-SITE.com';
$userData = array("username" => 'A_ADMIN_USERNAME', "password" => 'A_ADMIN_PASSWORD');
define('MAGENTO_SITE_URL',$magentoSiteUrl);
$ch = curl_init($magentoSiteUrl."/index.php/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch); // get access Token, use it for API call
$jobName = $_REQUEST['jobname'];
switch ($jobName){
case 'getproducts':
getProductsData($token);
break;
default:
echo "Not Job Name found in request";
break;
}
function getProductsData($accToken)
{
$searchCriteria='';
$searchCriteria .= 'searchCriteria[filter_groups][0][filters][0][field]=sku&';
$searchCriteria .= 'searchCriteria[filter_groups][0][filters][0][value]=ABC123&';
$searchCriteria .= 'searchCriteria[filter_groups][0][filters][0][condition_type]=eq';
$ch = curl_init(MAGENTO_SITE_URL."/index.php/rest/V1/products/search?".$searchCriteria);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($accToken)));
$result = curl_exec($ch);
echo $result;
}
step 2)
From your angular app (xyz.test.com) access to Magento API using URL like
www.my-magento-siteurl.com/apiconnect.php?jobname=getproducts