Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: BAD User is authenticated but not connected > outlook365 IMAP connection with oAuth token #482

Open
mitebu opened this issue Apr 10, 2024 · 0 comments

Comments

@mitebu
Copy link

mitebu commented Apr 10, 2024

Hi

I'm having an issue configuring an IMAP connection to an Outlook365 account with oAuth token.
First of all, I attach the steps configured in Microsoft Entra ID, with this guide: Microsoft Answers

After configuring and testing the output of the Powershell script, I can read and access without problems to the folders of the mailbox.
prueba imap censored

But when I'm configuring the PHP-IMAP library for this connection, I reach the conection (user is authenticated) but when I try to read the folders ($client->getFolders();) it throws the error.

		`include 'includes/php-imap/vendor/autoload.php'; 

		use Webklex\PHPIMAP\ClientManager;

		$CLIENT_ID="/* censored data */";
		$CLIENT_SECRET="/* censored data */";
		$TENANT="/* censored data */";
		$REFRESH_TOKEN="/* censored data */";

		$url= "https://login.microsoftonline.com/$TENANT/oauth2/v2.0/token";

		$param_post_curl = [ 
		'client_id'=>$CLIENT_ID,
		'client_secret'=>$CLIENT_SECRET,
		'refresh_token'=>$REFRESH_TOKEN,
		'grant_type'=>'refresh_token' ];

		$ch=curl_init();

		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($param_post_curl));
		curl_setopt($ch,CURLOPT_POST, 1);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
		//ONLY USE CURLOPT_SSL_VERIFYPEER AT FALSE IF YOU ARE IN LOCALHOST !!!

		$oResult=curl_exec($ch);

		if(!empty($oResult)){
			
			//The token is a JSON object
			$array_php_resul = json_decode($oResult,true);
			
			if( isset($array_php_resul["access_token"]) ){

				$access_token = $array_php_resul["access_token"];
				//$cm = new ClientManager($options = ["options" => ["debug" => true]]);                     
				$cm = new ClientManager();	 
              
				$client = $cm->make([
					'host'          => 'outlook.office365.com',                
					'port'          => 993,
					'encryption'    => 'ssl',
					'validate_cert' => false,
					'username'      => '/* censored data */',
					'password'      => $access_token,
					'protocol'      => 'imap',
					'authentication' => "oauth",
					"timeout" => 30,
					"extensions" => []
				]);
				
				try {
					//Connect to the IMAP Server
					$client->connect();
					//$client->checkConnection();
					$folders = $client->getFolders();
					
				}catch (Exception $e) {
					echo 'Exception : ',  $e->getMessage(), "\n";
				}

			} else {
				echo('Error : '.$array_php_resul["error_description"]); 
			}
		}`

After contacting Microsoft Azure support, their answer is that with Powershell works, the problem is in the library or code configured.
I've been searching for a solution with no result, maybe some1 had the same problem and can help me?
Adittional information needed please aske me for it.

Best regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant