Scripted Browser 401 Unauthorized to API

I am trying to create a scripted browser. The script successfully logs in and is able to traverse through the webpages fine , However i seem to get a 401 unauthorized on the api attached to the webpage. The user has permissions to access both UI and API . How do i combine the script below access to the api so I don’t get the 401 error .

The ‘clickelement MyEvents’ triggers an api call. Do i need to login to the api and create a promise chain ? .

Any help is much appreciated

const assert = require('assert'),
	By = $driver.By,
	browser = $browser.manage()
/** BEGINNING OF SCRIPT **/

console.log('Starting synthetics script: tester');
console.log('Default timeout is set to ' + (DefaultTimeout/1000) + ' seconds');

// Setting User Agent is not then-able, so we do this first (if defined and not default)
if (UserAgent && (0 !== UserAgent.trim().length) && (UserAgent != 'default')) {
  $browser.addHeader('User-Agent', UserAgent);
  console.log('Setting User-Agent to ' + UserAgent);
}

// Get browser capabilities and do nothing with it, so that we start with a then-able command
$browser.getCapabilities().then(function () { })
	.then(() => {
            logger.log(1, Url);
            return $browser.get(Url);
        })
	.then(() => {
            logger.log(2, "clickElement name=uEmail");
            return $browser.waitForAndFindElement(By.name("uEmail"), DefaultTimeout)
                .then(function (el) {
                    el.click();
                })
        })
	.then(() => {
            logger.log(3, 'type ' + Email);
            return $browser.waitForAndFindElement(By.name("uEmail"), DefaultTimeout)
                .then(function (el) {
                    el.clear();
                    el.sendKeys(Email);
                })
        })
	.then(() => {
            logger.log(4, "clickElement name=password");
            return $browser.waitForAndFindElement(By.name("password"), DefaultTimeout)
                .then(function (el) {
                    el.click();
                })
        })
	.then(() => {
            logger.log(5, "type Password");
            return $browser.waitForAndFindElement(By.name("password"), DefaultTimeout)
                .then(function (el) {
                    el.clear();
                    el.sendKeys(Password);
                })
        })
	.then(() => {
            logger.log(6, "clickElement id=gobutton");
            return $browser.waitForAndFindElement(By.id("gobutton"), DefaultTimeout)
                .then(function (el) {
                    el.click();
                })
        })
  .then(() => {
            logger.log(8, "clickElement link=My Events");
            return $browser.waitForAndFindElement(By.linkText("My Events"), DefaultTimeout)
                .then(function (el) {
                    el.click();
                })
        })
  
  .then(function() {
		logger.end();
		console.log('Browser script execution SUCCEEDED.');
	}, function(err) {
		logger.end();
		console.log ('Browser script execution FAILED.');
		throw(err);
	});

@bkelly2

It sounds like you need to dig into the specific API. 401 implies that the request is unauthorised, but, the fix for this depends on what the API expects.

I would suggest trying to hit that API via your local terminal to figure out the expectations. Whether it needs an API key or some other authenticating header.

Once you’ve got that figured out locally - it’ll give you a better understanding of what your Synthetics script requires.

But for now, there’s very little we in New Relic Support here can do to help, not knowing what the API expects or how the site works, we can’t offer much guidance.