Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Oauth2 Synthetics API Test

feature-idea
rfb

#1

Hi all,

I need to write a API test with Synthetics, but my App has to go through OAuthe2, with the grant type of authorization code. I’m pretty new to nodejs. Do we have any documentation for this?
I read the node modules link https://docs.newrelic.com/docs/synthetics/new-relic-synthetics/scripting-monitors/import-nodejs-modules
, and found that passport and authentication modules are not included?

What’s the simplest way of doing this?


#2

HI @tracywan_newbee - I have a sample API script which you can use which includes the authentication step :grin:


#3

Thanks @stefan_garnham

I saw your post but that won’t work for my case. The oauth2 login part I need is authentication-code with client id and secret id. But the library packages included by New Relic is limited, so lots of cookie parser and response parser cannot be used.

More specifically, it’s like I have a response and I need to extract the code on the right of =
…authorized…?code=…

I’m exploring the regex haven’t got it working yet

new RegExp("[?;]code=([^\"]*)")).exec(response);


#4

If you are after a method to manipulate cookies, this post on API cookies may help.


#5

@tracywan_newbee Let us know where you are at with this and if @stefan_garnham 's suggestions helped you get sorted! :blush:


#6

I was able to figure it out but I would prefer there’s more modules that could be added to the whitelist.
Such as
request-promise
http
cookie-parser


#7

Ahh definitely—good to know, @tracywan_newbee! Thanks for following up.

I’ll be sure to pass that input along as well. :thumbsup:


#8

Thought I’d add another example, this is just a simple template script for hitting an OAuth 2.0 secured API in a New Relic Synthetic API test: https://gist.github.com/lostphilosopher/899a5ccce78f692906d7ee3e330aa098

It was a bit tedious to figure out how to do this in https://github.com/request/request and the example already in this thread was too complex for what I was looking for.

Hope this helps someone.

var assert = require('assert');

function new_relic_callback(err, response, body) {
  assert.equal(response.statusCode, 200, 'Expected a 200 OK response');
};

function api_request_callback(err, response, body) {
  var parsed_body = JSON.parse(body); 
  
  var api_request = {
    url: 'https://example.com',
    headers: {
      'Authorization': 'Bearer ' + parsed_body["access_token"]
    }
  };

  $http.get(api_request, new_relic_callback);
};

var token_request = {
  url: 'https://example.com/oauth/token',
  form: {
    client_id: "YOUR_CLIENT_ID",
    client_secret: "YOUR_CLIENT_SECRET",
    grant_type: "client_credentials"
  }
};

$http.post(token_request, api_request_callback);

Multiple API calls in single synthetics
#9

I just want to say thank you so much for this. I was completely lost and this was exactly what I needed. Just plug and play!


#10

That is what we love to hear, @michael.brinkman! Thanks for letting us know you got what you needed. :blush: