Google Cloud 401 Error on request to DialogFlow despite correct credentials

I am attempting to make an application using Articulate Storyline that incorporates Google’s DialogFlow.

The error isn’t on the client side in Storyline, but I mention it’s what I’m using since the slides are published to https://360.articulate.com where the call is made from.

// Function to send a query to Dialogflow and retrieve the response
async function generateSentence(input) {
  const apiUrl = 'https://dialogflow.googleapis.com/v2/projects/(PROJECT_NAME)/agent/sessions/unique-session-id:detectIntent';
  const apiKey = '(API_KEY)';
  const languageCode = 'fr';

  const requestPayload = {
    queryInput: {
      text: {
        text: input,
        languageCode: languageCode,
      },
    },
  };

  try {
    const response = await fetch(apiUrl, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${apiKey}`,
      },
      body: JSON.stringify(requestPayload),
    });

    if (response.ok) {
      const data = await response.json();
      const fulfillmentText = data.queryResult.fulfillmentText;
      return fulfillmentText;
    } else {
      throw new Error('Request failed with status: ' + response.status);
    }
  } catch (error) {
    console.error('Error:', error);
    throw error;
  }
}

// Function to handle user input and display the output
async function handleInput() {
  const input = window.IATexte;
  try {
    const sentence = await generateSentence(input);
    console.log(sentence);
  } catch (error) {
    console.error('Error:', error);
  }
}

// Call the input handling function
handleInput();

(where I redacted my project name and api key)

I have verified that the API key is correct, and I have enabled the Dialogflow API in the Google Cloud Console for my project. However, when I make requests to the API using the API key, I receive the following response:

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

And the following in response headers:

www-authenticate: Bearer realm="https://accounts.google.com/", error="invalid_token"

I have also ensured that the DialogFlow API is enabled for my project.