I have been using k6 to load test a web application where I need to access gmail inbox for parsing. As googleapis and axios are not supported in k6 I created http requests using k6/http module. I was able to retrieve the access token but couldn’t access the mail inbox. Struck with this error

INFO[0001] {"error":{"code":401,"message":"Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","errors":[{"message":"Login Required.","domain":"global","reason":"required","location":"Authorization","locationType":"header"}],"status":"UNAUTHENTICATED","details":[{"domain":"googleapis.com","metadata":{"service":"gmail.googleapis.com","method":"caribou.api.proto.MailboxService.ListMessages"},"@type":"type.googleapis.com/google.rpc.ErrorInfo","reason":"CREDENTIALS_MISSING"}]}}

This is the manually written access token retrieval part

const payload = `client_id=${clientId}&client_secret=${clientSecret}&refresh_token=${refreshToken}&grant_type=refresh_token`;
  const tokenResponse = http.post(tokenUrl, payload, {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  const accessToken = tokenResponse.json().access_token;

It logged the accestoken in the console

const options = {
      url: 'https://gmail.googleapis.com/gmail/v1/users/[email protected]/messages',
      params: {
        q: `from:[email protected] to:${profilesEmailAddress}`,
      headers: {
        Authorization: `Bearer ${accessToken.access_token}`,
    const response = http.get(options.url, options.params, options.headers);

This is used to access the gmail inbox where I used http.get() instead of axios(). It couldn’t access the mail inbox and returns the error mentioned above. Can someone help me with this? What am I doing wrong? Why it isn’t able to access the mail body? I was able to get a response in Postman with the same logic cURL command.

curl -X GET 
  'https://gmail.googleapis.com/gmail/v1/users/[email protected]/messages?q=from%3Ano-reply%40rework.link%20to%[email protected]' 
  -H 'Authorization: Bearer <access_token>'