How to scrape emails using azure authentication and php encoding
P粉786800174
P粉786800174 2023-09-05 14:58:32
0
1
643
<p>We have an application written in PHP to get inbox emails from email id and it is working fine. This is a basic authentication application. Recently they have stopped Basic Authentication so we created an account on Azure to get new Authentication and based on that we want to scrape emails. </p> <p>We have created a code using the Application ID and Secret ID. When we open the page it redirects to the login page and that's the problem. We need it to automatically log in and grab emails. This is a cron process, so grabbing the email every time we need to enter a login is not a solution. </p> <p>https://xxxx.co/projects/test.php?action=login When we open this link, it will ask for login. We don't want that because we've already placed all the data like application ID, secret ID, and tenant ID. </p> <p>If we are already logged into Microsoft, then it will not ask for login but will grab the page via email. But when we open it in incognito mode, it asks for login.我们如何删除它并直接获取电子邮件</p> <pre class="brush:php;toolbar:false;"><?php $appid = "xxxxx"; $tennantid = "xxxxx"; $secret = "xxxxxx"; $login_url ="https://login.microsoftonline.com/".$tennantid."/oauth2/v2.0/authorize"; session_start (); $_SESSION['state']=session_id(); echo "<h1>MS OAuth2.0 Demo </h1><br>"; if (isset ($_SESSION['msatg'])){ echo "<h2>Authenticated ".$_SESSION["uname"]." </h2><br> "; echo '<p><a href="?action=logout">Log Out</a></p>'; } //end if session else echo '<h2><p>You can <a href="?action=login">Log In</a> with Microsoft</p></h2>'; if ($_GET['action'] == 'login'){ $params = array ('client_id' =>$appid, 'redirect_uri' =>'https://xxx.co/projects/test.php', 'response_type' =>'token', 'scope' =>'https://graph.microsoft.com/User.Read', 'state' =>$_SESSION['state']); header ('Location: '.$login_url.'?'.http_build_query ($params)); } echo ' <script> url = window.location.href; i=url.indexOf("#"); if(i>0) { url=url.replace("#","?"); window.location.href=url;} </script> '; if (array_key_exists ('access_token', $_GET)) { $_SESSION['t'] = $_GET['access_token']; $t = $_SESSION['t']; $ch = curl_init (); curl_setopt ($ch, CURLOPT_HTTPHEADER, array ('Authorization: Bearer '.$t, 'Conent-type: application/json')); curl_setopt ($ch, CURLOPT_URL, "https://graph.microsoft.com/v1.0/me/"); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); $rez = json_decode (curl_exec ($ch), 1); if (array_key_exists ('error', $rez)){ var_dump ($rez['error']); die(); } else { $_SESSION['msatg'] = 1; //auth and verified $_SESSION['uname'] = $rez["displayName"]; $_SESSION['id'] = $rez["id"]; } curl_close ($ch); header ('Location: https://xxxx.co/projects/test.php'); } if ($_GET['action'] == 'logout'){ unset ($_SESSION['msatg']); header ('Location: https://xxxx.co/projects/test.php'); }</pre> <p>当我们打开此代码时,它会要求登录。我们不希望这样。它将使用 php 直接抓取电子邮件</p>
P粉786800174
P粉786800174

reply all(1)
P粉426780515

Use OAuth 2.0 Authorization to authenticate the user and obtain an access token. And use it to call Microsoft Graph API to retrieve the user's email.

For your question, the login page may appear when you are not logged in. To resolve this issue, you need to use OAuth 2.0 client credentials instead of the authorization code. p>

Sample code to obtain an access token using client credentials.

$tenantId = 'your-tenant-id';
$client_id = 'your-client-id';
$client_secret = 'your-client-secret';
$resource = 'https://graph.microsoft.com';
$tokenEndpoint = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token';

$data = array(
    'grant_type' => 'client_credentials',
    'client_id' => $client_id,
    'client_secret' => $client_secret,
    'resource' => $resource
);

$options = array(
    'http' => array(
        'header' => "Content-type: application/x-www-form-urlencoded\r\n",
        'method' => 'POST',
        'content' => http_build_query($data)
    )
);

$context = stream_context_create($options);
$result = file_get_contents($tokenEndpoint, false, $context);
$token = json_decode($result)->access_token;

After you obtain the access token, you can use it to call the Microsoft Graph API and retrieve the user's email.

Sample code to retrieve user email.

php
$graphApiEndpoint = 'https://graph.microsoft.com/v1.0/me/messages';
$options = array(
    'http' => array(
        'header' => "Authorization: Bearer $token\r\n" .
                    "Content-type: application/json\r\n",
        'method' => 'GET'
    )
);

$context = stream_context_create($options);
$result = file_get_contents($graphApiEndpoint, false, $context);
$messages = json_decode($result)->value;

For more information, see MSDoc1 and MSDoc2.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template