I am trying to connect to Office 365 to retrieve emails.
I am using the Webklex library version 4.1.
I am able to obtain the authentication token to log into the email, but I always receive ‘connection setup failed’.
This is how I capture the authentication token.
private function getToken0Auth()
{
try {
$client = new GuzzleHttpClient();
// Solicitar o token do endpoint de OAuth2 do Microsoft
// laravel office
$response = $client->post('https://login.microsoftonline.com/'.env('tenant_id').'/oauth2/v2.0/token', [
'form_params' => [
'client_id' => env('client_id'),
'client_secret' => env('client_secret'),
'scope' => 'https://outlook.office365.com/.default',
'grant_type' => 'client_credentials',
],
]);
$body = json_decode($response->getBody());
if (!$body->access_token) {
return ['success' => false, 'message' => 'Não foi possivel gerar o token.'];
}
return ['success' => true, 'token' => $body->access_token];
} catch (Throwable $t) {
return ['success' => false, 'message' => $t->getMessage()];
}
}
This is an example of the token response:
eyJ0eXAiOiJKV1QiLCJub25jZSI6InNjUWJVeERrY3BvcjNsdVdaa080OERISDNsSDQwSmhEVk5QeXlVanBBb1EiLCJhbGciOiJSUzI1NiIsIng1dCI6Ikg5bmo1QU9Tc3dNcGhnMVNGeDdqYVYtbEI5dyIsImtpZCI6Ikg5bmo1QU9Tc3dNcGhnMVNGeDdqYVYtbEI5dyJ9.eyJhdWQiOiJodHRwczovL291dGxvb2sub2ZmaWNlMzY1LmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2IzNDdhMGY0LTA3MzQtNDI5Ni05NzU3LTc2NzJkOGFjYjJhNC8iLCJpYXQiOjE3MjU2NDU5ODYsIm5iZiI6MTcyNTY0NTk4NiwiZXhwIjoxNzI1NjQ5ODg2LCJhaW8iOiJFMmRnWUxnbllYVWhjbzlWUU02SGEvM0poNXMvQWdBPSIsImFwcF9kaXNwbGF5bmFtZSI6IkxhcmF2ZWwgT2ZmaWNlIDM2NSBJbnRlZ3JhdGlvbiIsImFwcGlkIjoiMzkwNjJiNjMtZGY4OC00M2ZmLWI3MTktYWQ5M2Q3MjNlNDgxIiwiYXBwaWRhY3IiOiIxIiwiaWRwIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvYjM0N2EwZjQtMDczNC00Mjk2LTk3NTctNzY3MmQ4YWNiMmE0LyIsImlkdHlwIjoiYXBwIiwib2lkIjoiZmFlZWFjYmEtZjcwZS00MmRjLThhZWYtYjdmNzFmNzBlM2ZiIiwicmgiOiIwLkFWZ0E5S0JIc3pRSGxrS1hWM1p5Mkt5eXBBSUFBQUFBQVBFUHpnQUFBQUFBQUFEeUFBQS4iLCJyb2xlcyI6WyJJTUFQLkFjY2Vzc0FzQXBwIiwiU01UUC5TZW5kQXNBcHAiXSwic2lkIjoiOWU3YjgxODctZmY5ZC00OWRiLTg1N2EtMWJiMDEyZGY4NjgwIiwic3ViIjoiZmFlZWFjYmEtZjcwZS00MmRjLThhZWYtYjdmNzFmNzBlM2ZiIiwidGlkIjoiYjM0N2EwZjQtMDczNC00Mjk2LTk3NTctNzY3MmQ4YWNiMmE0IiwidXRpIjoidWF5bHNEVkZ0VS1TN3luYUpVOEtBQSIsInZlciI6IjEuMCIsIndpZHMiOlsiMDk5N2ExZDAtMGQxZC00YWNiLWI0MDgtZDVjYTczMTIxZTkwIl0sInhtc19pZHJlbCI6IjcgMjgifQ.iZYBRa448-6E0oGNJU4FcXoeiDuhLTgDBQSMmJ-kyY1cOtsf3YHtl3b2HCYcRnanxLSa1v8WHxV2VR0KUH_FRUnDhAIVmHxztt039YvB3mk1znVXB7er6wDK_w07xRxH6ftvqo3ia8KPzXQXYGJ4iz2E8NyJYFlp8J_aj76fK2OncxHnTYe5HxyEkqWN50IKCTDoSFL_bEhmNLRhhAXm6O5od81KVC0sGz8BbhBD-QTLnoZRptb3SG1KTc0H-_uRvO8qAWYpwu3XCzNCETmiIuVVSyDUjafh6CinW8sAVm9kuBCtPpetzoYbljQlLjVyfE5Ppha8DmdFj2xtTYS4XA
After capturing the token, I use this function to connect to the IMAP server:
$oClient = Client::make([
'host' => 'outlook.office365.com',
'port' => 993,
'encryption' => 'ssl',
'validate_cert' => false,
'username' => 'my-email',
'password' => $response['token'], // Token OAuth2
'protocol' => 'imap',
'authentication' => 'oauth', // Definindo para autenticação OAuth
]);
$oClient->connect();
However, it always falls into the catch block, returning the same error: “connection setup failed”
In my Office 365 account, I had to register an app and grant permissions for access via IMAP.AccessAsApp
enter image description here
I also enabled IMAP for my mailbox.
enter image description here
Access to this application was created for any account.
I’m out of options at this point.