Error connect office365 with Webklex Laravel 11

Luiz Ceolin 0 Reputation points
2024-09-10T12:28:03.4666667+00:00

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 \GuzzleHttp\Client();
        // 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.eyJhdWQiOiJodHRwczovL291dGxvb2sub2ZmaWNlMzY1LmNvbSIsImlzcyI6Imh0dHBzOi8vc3RzLndpbmRvd3MubmV0L2IzNDdhMGY0LTA3MzQtNDI5Ni05NzU3LTc2NzJkOGFjYjJhNC8iLCJpYXQiOjE3MjU2NDU5ODYsIm5iZiI6MTcyNTY0NTk4NiwiZXhwIjoxNzI1NjQ5ODg2LCJhaW8iOiJFMmRnWUxnbllYVWhjbzlWUU02SGEvM0poNXMvQWdBPSIsImFwcF9kaXNwbGF5bmFtZSI6IkxhcmF2ZWwgT2ZmaWNlIDM2NSBJbnRlZ3JhdGlvbiIsImFwcGlkI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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'],
    'protocol'      => 'imap',
    'authentication' => '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

User's image

I also enabled IMAP for my mailbox.User's image

Access to this application was created for any account.

I'm out of options at this point.

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,273 questions
Outlook Management
Outlook Management
Outlook: A family of Microsoft email and calendar products.Management: The act or process of organizing, handling, directing or controlling something.
5,281 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,578 questions
0 comments No comments
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.