如何使用 azure 身份验证和 php 编码抓取电子邮件
P粉786800174
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;">&lt;?php $appid = &quot;xxxxx&quot;; $tennantid = &quot;xxxxx&quot;; $secret = &quot;xxxxxx&quot;; $login_url =&quot;https://login.microsoftonline.com/&quot;.$tennantid.&quot;/oauth2/v2.0/authorize&quot;; session_start (); $_SESSION['state']=session_id(); echo &quot;&lt;h1&gt;MS OAuth2.0 Demo &lt;/h1&gt;&lt;br&gt;&quot;; if (isset ($_SESSION['msatg'])){ echo &quot;&lt;h2&gt;Authenticated &quot;.$_SESSION[&quot;uname&quot;].&quot; &lt;/h2&gt;&lt;br&gt; &quot;; echo '&lt;p&gt;&lt;a href=&quot;?action=logout&quot;&gt;Log Out&lt;/a&gt;&lt;/p&gt;'; } //end if session else echo '&lt;h2&gt;&lt;p&gt;You can &lt;a href=&quot;?action=login&quot;&gt;Log In&lt;/a&gt; with Microsoft&lt;/p&gt;&lt;/h2&gt;'; if ($_GET['action'] == 'login'){ $params = array ('client_id' =&gt;$appid, 'redirect_uri' =&gt;'https://xxx.co/projects/test.php', 'response_type' =&gt;'token', 'scope' =&gt;'https://graph.microsoft.com/User.Read', 'state' =&gt;$_SESSION['state']); header ('Location: '.$login_url.'?'.http_build_query ($params)); } echo ' &lt;script&gt; url = window.location.href; i=url.indexOf(&quot;#&quot;); if(i&gt;0) { url=url.replace(&quot;#&quot;,&quot;?&quot;); window.location.href=url;} &lt;/script&gt; '; 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, &quot;https://graph.microsoft.com/v1.0/me/&quot;); 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[&quot;displayName&quot;]; $_SESSION['id'] = $rez[&quot;id&quot;]; } 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

全部回复(1)
P粉426780515

使用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

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号