ERR_TOO_MANY_REDIRECTS error when enabling AI providers

Created on 15 September 2024, 2 months ago
Updated 18 September 2024, about 2 months ago

Problem/Motivation

When attempting to enable AI providers (e.g., OpenAI) in Drupal 10.3, an "ERR_TOO_MANY_REDIRECTS" error occurs. The log reports an AccessDeniedHttpException due to an invalid authentication method, causing redirect loops and preventing provider configuration.
I am using a webspace with PHP 8.3 on Cloudways, with Digital Ocean as the hosting provider.

Here is the error in database log:

Path: /user/login?destination=/admin/config/ai/providers/openai. Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: The used authentication method is not allowed on this route. in Drupal\Core\EventSubscriber\AuthenticationSubscriber->onExceptionAccessDenied()

Steps to reproduce

  1. Install Drupal 10.3 with the AI and Key modules.
  2. Configure API keys using the Key module (via configuration storage).
  3. Attempt to enable an AI provider (e.g., OpenAI).
  4. Observe the redirect loop and error page.

Proposed resolution

Probably is a bug related to my environment. Please help me to investigate which the reason(s) should be, maybe other people is experiencing the same problem.

🐛 Bug report
Status

Closed: works as designed

Version

1.0

Component

Providers

Created by

🇮🇹Italy sepa_cleversoft

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @sepa_cleversoft
  • 🇩🇪Germany marcus_johansson

    Hi sepa_cleversoft.

    Could you try this file (but change the OpenAI key to your key). Please verify it for yourself of course, don't trust me before uploading it :)

    But this is a plain vanilla curl call to OpenAI models listing. Just save it in a file and visit it or do `php filename.php`

    // Your OpenAI API key
    $apiKey = 'YOUR_OPENAI_API_KEY';
    
    // Initialize cURL
    $ch = curl_init();
    
    // Set cURL options
    curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/models');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
      'Authorization: Bearer ' . $apiKey,
      'Content-Type: application/json'
    ]);
    
    // Execute the request
    $response = curl_exec($ch);
    
    // Check for errors
    if (curl_errno($ch)) {
      echo 'Error:' . curl_error($ch);
    } else {
      // Decode and print the response
      $models = json_decode($response, true);
      print_r($models);
    }
    
    // Close the cURL session
    curl_close($ch);
    
    

    If you see error with the same error, it is something with the host and you should reach out to them, if not its seems like some module is using the AuthenticationSubscriber wrongly (or we have a bug).

    OAuth, JWT, Custom Authentication are some modules using this subscriber.

  • 🇩🇪Germany marcus_johansson

    Oh, and replace `YOUR_OPENAI_API_KEY` with your actual key of course :)

  • 🇮🇹Italy sepa_cleversoft

    With your code I get the list of available models. Indeed, I was able also to run openai module without errors. Thank you for your support.

  • 🇩🇪Germany marcus_johansson

    So, is this solved or did you mean the OpenAI module worked, but not the AI module + OpenAI provider?

  • 🇮🇹Italy sepa_cleversoft

    No, it's not solved. The code you provided worked well, so the problem for me is related to the module or something related. Maybe also the module key can be involved? How can I debug the error better?

  • 🇩🇪Germany marcus_johansson

    Three things I can think of right of the bat that would give more info on this:

    Would it be possible for you to set it up locally and see if you can replicate the issue? Is there anything in the log messages? Where is it trying to redirect to?

  • 🇮🇹Italy sepa_cleversoft

    I found the issue. Was not directly related to ai module, but instead was a different module (services_api_key_auth) that was broking the website. Uninstalled it, it worked well.

    Maybe can be useful for someone else, for me the issue is closed. Thank you.

  • Status changed to Closed: works as designed about 2 months ago
  • 🇩🇪Germany marcus_johansson

    Great to hear that you found it. Closed the ticket.

Production build 0.71.5 2024