Office 365开发者试用订阅的自动续签「OutLook」
现在Office 365开发者订阅也叫E5,E5的自动续签方式就是调用API,具体的不知道,目前使用Oneindex就可以了,但是我不知道从哪里听来的实际自动续订需要用的是OutLook API,出于忧患之心,这里还是整一下。
开始之前
首先打开Azure的注册应用界面添加一个应用
https://portal.azure.com/#blade/Microsoft_AAD_RegisteredApps/ApplicationsListBlade
名称随便填
然后后面的URI填后面的文件地址,文件是PHP文件,自行发挥(这个URI在后面需要用到)
记好这里的ID和新建的密码
然后就是代码了
上传好代码,代码的前面几行是需要设置的,$client_id
填ID,$client_secret
填设置的密码,$redirect_uri
填URI
<?
$client_id = '';
$client_secret = '';
$redirect_uri = '';
function post($url = "", $data = array())
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
function get($url = "", $headers = array())
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
$flag = 0;
if (isset($_GET['code'])) {
$code = $_GET['code'];
// die($code);
} else {
$file = fopen("code.txt", "r");
$flag = 1;
$code = fread($file, filesize("code.txt"));
fclose($file);
}
$data = array(
'redirect_uri' => $redirect_uri,
'client_id' => $client_id,
'client_secret' => $client_secret
);
if ($flag) {
$data['grant_type'] = 'refresh_token';
$data['refresh_token'] = $code;
} else {
$data['grant_type'] = 'authorization_code';
$data['code'] = $code;
}
// var_dump($data);
$url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
$res = post($url, $data);
// echo $res;
$res = json_decode($res, true);
$access_token = $res['access_token'];
$refresh_token = $res['refresh_token'];
// echo $access_token;
$file = fopen("code.txt", "w");
fwrite($file, $refresh_token);
fclose($file);
// var_dump($res);
$url = 'https://graph.microsoft.com/v1.0/me/messages?$select=sender,subject';
$headers = array(
'Accept: application/json',
'Authorization: ' . $access_token
);
$list = json_decode(get($url, $headers),true);
$list = $list['value'];
$headers = array(
'Authorization: ' . $access_token,
'Prefer: outlook.body-content-type' . 'html'
);
foreach ($list as $item){
$mail = json_decode(get('https://graph.microsoft.com/v1.0/me/messages/'.$item['id'],$headers),true);
$subject = $mail['subject'];
$body = $mail['body'];
$sender = $mail['sender']['emailAddress'];
$name = $sender['name'];
$address = $sender['address'];
echo '来自'.$name;
echo "<";
echo $address;
echo ">";
echo ":\n";
echo '主题:'.$subject."\n正文:\n///\n";
echo $body['content'];
echo "\n\\\\\\\n----------------------------------------------------------------------------\n";
}
然后把相应的信息填好后访问这个地址并登录一个子账号,使用该代码可能需要在PHP文件相同目录下手动创建一个code.txt
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=【上面的ID】&redirect_uri=【访问PHP文件的URI地址】&response_type=code&scope=openid+Mail.Read+offline_access
自动化
如果没有提示错误之类的就代表可以使用了,最后就是自动化了,使用定时任务来访问这个,频率的话不要太低,10分钟一次都可以,好像自动续订是以频率为阈值的。