BAD User is authenticatedbut not connected Error When Connecting to IMAP with OAuth on Outlook Office 365 in PHP

28 Views Asked by At

I'm attempting to connect to an Outlook Office 365 IMAP server using OAuth authentication in a PHP application, but I'm encountering the following error after successful authentication: BAD User is authenticated but not connected.

Here is a snippet of the code I'm using to establish the connection:

    $data = array(
    'grant_type' => 'password',
    'client_id' => $client_id,
    'scope' => 'https://outlook.office365.com/IMAP.AccessAsUser.All',
    'username' => $username,
    'password' => $password,
    'client_secret' => $secret
);

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://login.microsoftonline.com/cae7d061-08f3-40dd-80c3-3c0b8889224a/oauth2/v2.0/token",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => http_build_query($data),
    CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded",
        "Authorization: Basic " . base64_encode("$client_id:$secret")
    ),
));

$response = curl_exec($curl);

curl_close($curl);

$token_data = json_decode($response, true);



if(isset($token_data['access_token'])) {
    $access_token = $token_data['access_token'];

    // Utiliser l'accès token pour faire une requête à l'API Graph Microsoft

$cm = new ClientManager();



$client = $cm->make([
    'host' => 'outlook.office365.com',
    'port' => 993,
    'encryption' => 'ssl',
    'validate_cert' => true,
    'username' => $username,
    'password' =>  $access_token,
    'protocol' => 'imap',
    'authentication' => "oauth"
]);
try {
$client->connect();
$folders = $client->getFolders($hierarchical = true);
    
    // Vous pouvez continuer à exécuter d'autres opérations sur la boîte aux lettres ici
} catch (\Exception $e) {
    // En cas d'erreur, capturer et afficher l'exception
    echo "Erreur lors de la connexion au serveur IMAP : " . $e->getMessage();
}



   
    
} else {
    echo "Erreur lors de l'obtention du jeton d'accès";
}

I'm obtaining an access token via OAuth2 following Microsoft's documentation, and the authentication appears to succeed. However, connecting to the IMAP server fails with the aforementioned error.

I'm attempting to connect to an Outlook Office 365 IMAP server using OAuth authentication in a PHP application, but I'm encountering the following error after successful authentication: BAD User is authenticated but not connected.

Here is a snippet of the code I'm using to establish the connection:

    $data = array(
    'grant_type' => 'password',
    'client_id' => $client_id,
    'scope' => 'https://outlook.office365.com/IMAP.AccessAsUser.All',
    'username' => $username,
    'password' => $password,
    'client_secret' => $secret
);

$curl = curl_init();

curl_setopt_array($curl, array(
    CURLOPT_URL => "https://login.microsoftonline.com/cae7d061-08f3-40dd-80c3-3c0b8889224a/oauth2/v2.0/token",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => "POST",
    CURLOPT_POSTFIELDS => http_build_query($data),
    CURLOPT_HTTPHEADER => array(
        "Content-Type: application/x-www-form-urlencoded",
        "Authorization: Basic " . base64_encode("$client_id:$secret")
    ),
));

$response = curl_exec($curl);

curl_close($curl);

$token_data = json_decode($response, true);



if(isset($token_data['access_token'])) {
    $access_token = $token_data['access_token'];

    // Utiliser l'accès token pour faire une requête à l'API Graph Microsoft

$cm = new ClientManager();



$client = $cm->make([
    'host' => 'outlook.office365.com',
    'port' => 993,
    'encryption' => 'ssl',
    'validate_cert' => true,
    'username' => $username,
    'password' =>  $access_token,
    'protocol' => 'imap',
    'authentication' => "oauth"
]);
try {
$client->connect();
$folders = $client->getFolders($hierarchical = true);
    
    // Vous pouvez continuer à exécuter d'autres opérations sur la boîte aux lettres ici
} catch (\Exception $e) {
    // En cas d'erreur, capturer et afficher l'exception
    echo "Erreur lors de la connexion au serveur IMAP : " . $e->getMessage();
}



   
    
} else {
    echo "Erreur lors de l'obtention du jeton d'accès";
}

I'm obtaining an access token via OAuth2 following Microsoft's documentation, and the authentication appears to succeed. However, connecting to the IMAP server fails with the aforementioned error.

0

There are 0 best solutions below