如何使用 azure 身份验证和 php 编码抓取电子邮件
P粉786800174
2023-09-05 14:58:32
[PHP讨论组]
<p>我们有一个用 PHP 编写的应用程序,用于从电子邮件 ID 中获取收件箱电子邮件,该应用程序运行良好。这是一个基本的身份验证应用程序。最近他们已经停止了基本身份验证,因此我们在 Azure 上创建了一个帐户来获得新的身份验证,并基于此我们想要抓取电子邮件。</p>
<p>我们已经使用应用程序 ID 和秘密 ID 创建了一个代码。
当我们打开页面时,它会重定向到登录页面,这就是问题所在。
我们需要它自动登录并抓取电子邮件。
这是一个 cron 进程,因此每次我们需要输入登录时,抓取电子邮件并不是解决方案。</p>
<p>https://xxxx.co/projects/test.php?action=login
当我们打开此链接时,它会要求登录。我们不希望这样,因为我们已经放置了应用程序 ID、秘密 ID 和租户 ID 等所有数据。</p>
<p>如果我们已经登录微软,那么它不会要求登录,而是通过电子邮件抓取页面。但是当我们以隐身模式打开时,它会要求登录。我们如何删除它并直接获取电子邮件</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>
使用
OAuth 2.0授权对用户进行身份验证并获取访问令牌。并使用它调用Microsoft Graph API来检索用户的电子邮件。对于您的问题,在您未登录时可能会出现登录页面。要解决此问题,您需要使用
OAuth 2.0 客户端凭据而不是授权代码。 p>使用客户端凭据获取访问令牌的示例代码。
$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;获取访问令牌后,您可以使用它来调用
Microsoft Graph API并检索用户的电子邮件。检索用户电子邮件的示例代码。
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;有关详细信息,请参阅 MSDoc1 和 MSDoc2。