Received an error from the MongoDB Atlas endpoint: "multiple authentication methods used"

I am currently attempting to authenticate using an API key at the app service endpoint in MongoDB Atlas, but I encountered an error:

{
    "error": "multiple authentication methods used",
    "error_code": "InvalidParameter",
    "link": "[log link]"
}

I am using the ‘POST’ method. I have tried to investigate this issue on several forums, and they have suggested ensuring that only the API key is active, which I have already done. Strangely, I have used a similar approach with the API key in several previous codes, and it worked successfully, providing a ‘success’ response.

Here is the code I am using:

function register($req)
{
    global $apiKey;

    $url = "https://asia-south1.gcp.data.mongodb-api.com/app/application-0-gfdpr/endpoint/user";
    $init = curl_init($url);
    curl_setopt($init, CURLOPT_HTTPHEADER, [
        "apikey: " . $apiKey,
        "Content-Type: application/json",
    ]);
    curl_setopt($init, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($init, CURLOPT_CUSTOMREQUEST, 'POST');
    $data = json_encode([
        "email" => $req['email'],
        "username" => $req['username'],
        "password" => password_hash($req['password'], PASSWORD_DEFAULT),
    ]);
    curl_setopt($init, CURLOPT_POSTFIELDS, $data);
    $response = curl_exec($init);
    curl_close($init);
    var_dump($response);
}

I suspect that what this is doing and what you think it is doing are two different things, and further that the Content-Type header doesn’t match this clause.

Review PHP: curl_setopt - Manual

The problem has been resolved, I replaced the key (username, password) inside the body with anything other than username/password.

thanks…

1 Like