【笔记】office365开发者计划--使用api读取outlook邮件
本帖最后由 风一样自由 于 2021-9-13 11:02 编辑## 参考手册或代码:
**1、azure注册应用,获取client_id和client_secret:**
https://aad.portal.azure.com/
**2、获取授权代码及令牌:**
https://docs.microsoft.com/zh-cn/azure/active-directory/develop/v2-oauth2-auth-code-flow
**3、Microsoft Graph REST API v1.0手册:**
https://docs.microsoft.com/zh-cn/graph/api/user-list-messages?view=graph-rest-1.0&tabs=http
**4、Graph 浏览器,直观体验Graph API:**
https://developer.microsoft.com/zh-cn/graph/graph-explorer
**5、参考代码:**OneIndex源码中/lib/onedrive.php
## 完成:index.php curl.php
#### index.php
```
<?php
// 获取当前url,在azure注册应用时重定向url填入此url
// $redirect_url = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
// die($redirect_url);
include_once("curl.php");
const API_URL = 'https://graph.microsoft.com/v1.0';// api
const CLIENT_ID = 'azure注册应用后得到client_id';// 应用ID
const CLIENT_SECRET = 'azure注册应用后得到client_secret';// 应用密码
const SCOPE = 'openid offline_access email Mail.Read Mail.Read.Shared Mail.Read.Shared Mail.ReadBasic Mail.ReadWrite Mail.ReadWrite.Shared Mail.Send Mail.Send.Shared';// 应用权限;openid:没有访问api可能会报错;offline_access:获取refresh_token的必须;其余权限根据所需在azure所注册的应用中添加或删除
// microsoft1: 获取授权代码code
function oauth(){
// 获取当前url作为office应用的重定向url
$redirect_url = $_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
if(!empty($_GET['state']) && $_GET['state'] == md5($redirect_url)){
get_token($redirect_url);
header('Location: '.$redirect_url);
}else{
$url = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize?'.'client_id='.CLIENT_ID.'&response_type=code&redirect_uri='.$redirect_url.'&response_mode=query&scope='.SCOPE.'&state='.md5($redirect_url);
header("Location: {$url}");
}
}
// microsoft2:通过code兑换access_token及refresh_token
function get_token($redirect_url){
$code = $_GET['code'];
$url = "https://login.microsoftonline.com/common/oauth2/v2.0/token/";
$data = "client_id=".CLIENT_ID."&client_secret=".CLIENT_SECRET."&redirect_uri=".$redirect_url."&scope=".SCOPE."&code=".$code."&grant_type=authorization_code";
$headerArray = array("Content-Type: application/x-www-form-urlencoded");
$result = Curl::posturl($url,$data,$headerArray);
// 加上access_token过期时间戳
$result['expires_on'] = time() + $result['expires_in'];
$token_jsonstr = json_encode($result);
file_put_contents('token.json',$token_jsonstr);
}
// 通过 refresh_token刷新access_token
function refresh_token($refresh_token){
$url = "https://login.microsoftonline.com/common/oauth2/v2.0/token/";
$data = "client_id=".CLIENT_ID."&client_secret=".CLIENT_SECRET."&refresh_token={$refresh_token}&grant_type=refresh_token";
$headerArray = array("Content-Type: application/x-www-form-urlencoded");
$result = Curl::posturl($url,$data,$headerArray);
// 加上access_token过期时间戳
$result['expires_on'] = time() + $result['expires_in'];
$token_jsonstr = json_encode($result);
file_put_contents('token.json',$token_jsonstr);
return $result['access_token'];
}
// 检查access_token是否过期
function check_token(){
$token = file_get_contents("token.json");
if(empty($token) || !isset(json_decode($token,true)['access_token'])){
oauth();
}
$token = json_decode($token,true);
// 留10分钟的安全期
if(time()+600 < $token['expires_on']){
$access_token = $token['access_token'];
}else{
$access_token = refresh_token($token['refresh_token']);
}
return $access_token;
}
// 读取邮件
function readmail(){
$access_token = check_token();
$url = API_URL."/me/messages";
$headerArray = array("authorization: Bearer {$access_token}");
$result = Curl::geturl($url,$headerArray);
return $result;
}
// 列出邮件文件夹
function mailfloder(){
$access_token = check_token();
$url = API_URL."/me/mailFolders";
$headerArray = array("authorization: Bearer {$access_token}","Content-Type: application/json");
$result = Curl::geturl($url,$headerArray);
return $result;
}
$mailData = readmail();
// $mailfloder = mailfloder();
echo "<pre>";
var_dump($mailData);
// var_dump($mailfloder);
```
### curl.php
```
<?php
class Curl {
public static function geturl($url,$headerArray)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
$output = curl_exec($curl);
curl_close($curl);
$output = json_decode($output, true);
return $output;
}
public static function posturl($url, $data,$headerArray)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output, true);
}
public static function puturl($url, $data)
{
$data = json_encode($data);
$curl = curl_init(); //初始化CURL句柄
curl_setopt($curl, CURLOPT_URL, $url); //设置请求的URL
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT"); //设置请求方式
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); //设置提交的字符串
$output = curl_exec($curl);
curl_close($curl);
return json_decode($output, true);
}
public static function delurl($put_url, $data)
{
$data = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $put_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($curl);
curl_close($curl);
$output = json_decode($output, true);
}
public static function patcurlurl($url, $data)
{
$data = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATcurl");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($curl);
curl_close($curl);
$output = json_decode($output);
return $output;
}
}
```
先收藏一下,谢谢楼主 支持,感谢分享
页:
[1]