如何在 PHP 中使用图表 API 更新 Exchange 365 自动回复
How To Update Exchange365 Autoreplies Using Graph API In PHP
我需要更新 Exchange 中的用户自动回复(外出)邮箱设置。我已准备好所有身份验证代码并且可以正常工作。现在我只需要告诉我要更新什么。
C# 示例是:
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var mailboxSettings = new MailboxSettings
{
AutomaticRepliesSetting = new AutomaticRepliesSetting
{
Status = AutomaticRepliesStatus.Scheduled,
ScheduledStartDateTime = new DateTimeTimeZone
{
DateTime = "2016-03-20T18:00:00",
TimeZone = "UTC"
},
ScheduledEndDateTime = new DateTimeTimeZone
{
DateTime = "2016-03-28T18:00:00",
TimeZone = "UTC"
}
}
};
var me = new User();
me.MailboxSettings = mailboxSettings;
await graphClient.Me
.Request()
.UpdateAsync(me);
我不确定如何使用图表 API 将其转换为 PHP。我试图找到有关如何使用 PHP API 执行此操作的文档,但没有成功。
这是我想出的 PHP 代码:
include "../../vendor/autoload.php";
$code=$_GET["code"];
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
$oauthClient = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => "clientid",
'clientSecret' => "secret",
'redirectUri' => "redirect",
'urlAuthorize' => "auth",
'urlAccessToken' => "access",
'urlResourceOwnerDetails' => '',
'scopes' => 'offline_access MailboxSettings.ReadWrite'
]);
try {
// Make the token request
$accessToken = $oauthClient->getAccessToken('authorization_code', [
'code' => $code
]);
$token=$accessToken->getToken();
$graph = new Graph();
$graph->setAccessToken($token);
$user = $graph->createRequest('GET', '/me/mailboxSettings')
->setReturnType(Model\MailboxSettings::class)
->execute();
$mailboxSettings = new Model\MailboxSettings();
$start=new Model\DateTimeTimeZone();
$start->DateTime = "2019-07-03T18:00:00";
$start->TimeZone = "America/New_York";
$end=new Model\DateTimeTimeZone();
$end->DateTime = "2019-07-04T18:00:00";
$end->TimeZone = "America/New_York";
$replySettings = new Model\AutomaticRepliesSetting($propDict);
$replySettings->setStatus(Model\AutomaticRepliesStatus::SCHEDULED);
$replySettings->setScheduledStartDateTime($start);
$replySettings->setScheduledEndDateTime($end);
$replySettings->setInternalReplyMessage("I'm out of the office");
$replySettings->setExternalReplyMessage("I'm out of the office");
$mailboxSettings->setAutomaticRepliesSetting($replySettings);
}
catch (League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
exit('ERROR getting tokens: '.$e->getMessage());
}
这段代码没有给我任何错误,但我不确定如何实际发送更新请求来更新设置。
另外,有没有办法让访问令牌持久化?我想要完成的是:
用户输入休假请求,输入他们想要的外出消息。我会有一个 cron 作业 运行,可以在他们不在办公室时更新他们的不在办公室消息。这可能吗?
实际上,GraphServiceClient 最终会调用 Microsoft Graph API (https://docs.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0)
有一个API可以修改邮箱设置。通过在其中设置 automaticRepliesSetting,您可以修改自动回复设置。您可以在该页面上找到示例:
注:
Microsoft Graph API 受 Azure AD 保护。您需要先获取访问令牌。如何获取auth token,请参考官方教程:Get auth token
更新:
您可以通过以下方式提出补丁请求:
<?php
$token = "your get with adal php sdk";
$authUrl = 'https://graph.microsoft.com/v1.0/me/mailboxSettings';
$ch = curl_init();
$headers = [
"Content-Type:application/json",
"Authorization:Bearer $token"
];
$data = array(
"@odata.context" => "https://graph.microsoft.com/v1.0/$metadata#Me/mailboxSettings",
"automaticRepliesSetting" => array (
"status" => "scheduled" ,
"externalAudience" => "all",
"scheduledStartDateTime" => array (
"dateTime" => "2019-07-18T05:00:00.0000000",
"timeZone" => "UTC"
),
"scheduledEndDateTime" => array (
"dateTime" => "2019-07-19T06:00:00.0000000",
"timeZone" => "UTC"
) ,
"internalReplyMessage" => "This is the internalReplyMessage.",
"externalReplyMessage" => "This is the externalReplyMessage."
)
);
$postdata = json_encode($data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $authUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$strResponse = curl_exec($ch);
$curlErrno = curl_errno($ch);
if ($curlErrno) {
$curlError = curl_error($ch);
throw new Exception($curlError);
}
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// $objResponse = json_decode($strResponse);
print_r($http_status."\n");
print_r($strResponse."\n");
?>
结果:
在outlook中,我可以看到自动回复设置已成功更新。
我需要更新 Exchange 中的用户自动回复(外出)邮箱设置。我已准备好所有身份验证代码并且可以正常工作。现在我只需要告诉我要更新什么。
C# 示例是:
GraphServiceClient graphClient = new GraphServiceClient( authProvider );
var mailboxSettings = new MailboxSettings
{
AutomaticRepliesSetting = new AutomaticRepliesSetting
{
Status = AutomaticRepliesStatus.Scheduled,
ScheduledStartDateTime = new DateTimeTimeZone
{
DateTime = "2016-03-20T18:00:00",
TimeZone = "UTC"
},
ScheduledEndDateTime = new DateTimeTimeZone
{
DateTime = "2016-03-28T18:00:00",
TimeZone = "UTC"
}
}
};
var me = new User();
me.MailboxSettings = mailboxSettings;
await graphClient.Me
.Request()
.UpdateAsync(me);
我不确定如何使用图表 API 将其转换为 PHP。我试图找到有关如何使用 PHP API 执行此操作的文档,但没有成功。
这是我想出的 PHP 代码:
include "../../vendor/autoload.php";
$code=$_GET["code"];
use Microsoft\Graph\Graph;
use Microsoft\Graph\Model;
$oauthClient = new \League\OAuth2\Client\Provider\GenericProvider([
'clientId' => "clientid",
'clientSecret' => "secret",
'redirectUri' => "redirect",
'urlAuthorize' => "auth",
'urlAccessToken' => "access",
'urlResourceOwnerDetails' => '',
'scopes' => 'offline_access MailboxSettings.ReadWrite'
]);
try {
// Make the token request
$accessToken = $oauthClient->getAccessToken('authorization_code', [
'code' => $code
]);
$token=$accessToken->getToken();
$graph = new Graph();
$graph->setAccessToken($token);
$user = $graph->createRequest('GET', '/me/mailboxSettings')
->setReturnType(Model\MailboxSettings::class)
->execute();
$mailboxSettings = new Model\MailboxSettings();
$start=new Model\DateTimeTimeZone();
$start->DateTime = "2019-07-03T18:00:00";
$start->TimeZone = "America/New_York";
$end=new Model\DateTimeTimeZone();
$end->DateTime = "2019-07-04T18:00:00";
$end->TimeZone = "America/New_York";
$replySettings = new Model\AutomaticRepliesSetting($propDict);
$replySettings->setStatus(Model\AutomaticRepliesStatus::SCHEDULED);
$replySettings->setScheduledStartDateTime($start);
$replySettings->setScheduledEndDateTime($end);
$replySettings->setInternalReplyMessage("I'm out of the office");
$replySettings->setExternalReplyMessage("I'm out of the office");
$mailboxSettings->setAutomaticRepliesSetting($replySettings);
}
catch (League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
exit('ERROR getting tokens: '.$e->getMessage());
}
这段代码没有给我任何错误,但我不确定如何实际发送更新请求来更新设置。
另外,有没有办法让访问令牌持久化?我想要完成的是: 用户输入休假请求,输入他们想要的外出消息。我会有一个 cron 作业 运行,可以在他们不在办公室时更新他们的不在办公室消息。这可能吗?
实际上,GraphServiceClient 最终会调用 Microsoft Graph API (https://docs.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0)
有一个API可以修改邮箱设置。通过在其中设置 automaticRepliesSetting,您可以修改自动回复设置。您可以在该页面上找到示例:
注:
Microsoft Graph API 受 Azure AD 保护。您需要先获取访问令牌。如何获取auth token,请参考官方教程:Get auth token
更新:
您可以通过以下方式提出补丁请求:
<?php
$token = "your get with adal php sdk";
$authUrl = 'https://graph.microsoft.com/v1.0/me/mailboxSettings';
$ch = curl_init();
$headers = [
"Content-Type:application/json",
"Authorization:Bearer $token"
];
$data = array(
"@odata.context" => "https://graph.microsoft.com/v1.0/$metadata#Me/mailboxSettings",
"automaticRepliesSetting" => array (
"status" => "scheduled" ,
"externalAudience" => "all",
"scheduledStartDateTime" => array (
"dateTime" => "2019-07-18T05:00:00.0000000",
"timeZone" => "UTC"
),
"scheduledEndDateTime" => array (
"dateTime" => "2019-07-19T06:00:00.0000000",
"timeZone" => "UTC"
) ,
"internalReplyMessage" => "This is the internalReplyMessage.",
"externalReplyMessage" => "This is the externalReplyMessage."
)
);
$postdata = json_encode($data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $authUrl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$strResponse = curl_exec($ch);
$curlErrno = curl_errno($ch);
if ($curlErrno) {
$curlError = curl_error($ch);
throw new Exception($curlError);
}
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// $objResponse = json_decode($strResponse);
print_r($http_status."\n");
print_r($strResponse."\n");
?>
结果:
在outlook中,我可以看到自动回复设置已成功更新。