Making a call to a gRPC service that includes a bearer token

Hello,

How do a make a call to a gRPC service that includes a bearer token?


import { check, sleep } from 'k6';

import http from 'k6/http'

const client = new grpc.Client();

client.load(['definitions'], '*.Services.*GrpcService.proto');

export default ()=> {

    client.connect('url.*:5050', {

    });

const data = {

    "*List": [ '*', '*' ],

   

  };

const response = client.invoke('Package.Service/Method', data);

check(response, {

    'status is OK': (r) => r && r.status === grpc.StatusOK,

  });

  console.log(JSON.stringify(response.message));

  client.close();

  sleep(1);

};

Hi @jbx
Maybe this example will help you:

import grpc from 'k6/net/grpc';
import { check } from 'k6';

const client = new grpc.Client();
client.load([], 'authorization.proto', 'route_guide.proto');

export function setup() {
  client.connect('auth.googleapis.com:443');
  const resp = client.invoke('google.cloud.authorization.v1.AuthService/GetAccessToken', {
    username: 'john.smith@k6.io',
    password: 'its-a-secret',
  });
  client.close();
  return resp.message.accessToken;
}

export default (token) => {
  client.connect('route.googleapis.com:443');
  const headers = {
    authorization: `bearer ${token}`,
  };
  const response = client.invoke(
    'google.cloud.route.v1.RoutingService/GetFeature',
    {
      latitude: 410248224,
      longitude: -747127767,
    },
    { headers }
  );
  check(response, { 'status is OK': (r) => r && r.status === grpc.StatusOK });
  client.close();
};

Thank you, the tests I’m working with use clientId and clientSecret to get the bearer token. I have this working for 1 test.

export function Token(){

    let url = 'https://auth.dev.pollos-hermanos.cloud/oauth/token';

    const requestBody = {

    client_id: 'ebVDxQfxlWTsGxzpmECrap',

    client_secret: 'PkngambNrwyEpeChuPoJMgzQnnbWxHIUwRPCPYQVqhuA',

    scope: 'pollos:hermanos',

    grant_type: 'client_credentials',

    audience: 'https://api.dev.pollos-hermanos.cloud/public'

    }

const response = http.post(url, requestBody);

console.log('bearerToken success');

return response.json();

}

the token is then added to params

const token = Token().access_token;

const params = {

  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${token}`,
  }
}

And then passed into the grpc invoke

const response = client.invoke('Pollos.Hermanos.Contracts.v1.Services.PollosService/GetPollosById', data, params);