【问题标题】:Access is denied. Check credentials and try again outlook API and microsoft graph访问被拒绝。检查凭据并重试 Outlook API 和 microsoft graph
【发布时间】:2022-03-03 14:46:35
【问题描述】:

你好,我是一个项目的一部分,我遇到了困难 这是以前的帖子 [解决自] :Empty Payload. JSON content expected (guzzle, php, azur active directory and outlook)azur-active-directory-and-out

快速总结: 在这个项目中,您必须填写发送信息的表格 安排一次关于前景的会议(日期、地点、标题...)。 json 流和正确格式的声音标记。我可以发送信息,但我无权访问 代码将在下面显示供您查看。

现在我有这个错误:

{"error":{"code":"ErrorAccessDenied","message":"Access is denied. Check credentials and 
try again."}}

这是页面的代码

第 1 页:

<form action="action.php" method="post">
    <div class="row" style="margin:10px;">
        <label for="Summary" class="col-sm-3">Subject :</label>
        <input type="text" class="col-sm-1 form-control" name="Subject" value="" autocomplete="off" required>
    </div>
    <div class="row" style="margin:10px;">
        <label for="Description" class="col-sm-3">Content:</label>
        <input type="datetime" class="col-sm-1 form-control" name="Content" value="" autocomplete="off" required>
    </div>
    <div class="row" style="margin:10px;">
        <label for="StartDateTime" class="col-sm-3">jour de la réunion :</label>
        <input type="date" class="col-sm-1 form-control" name="StartDateTime" value="" autocomplete="off" required>
    </div>
    <div class="row" style="margin:10px;">
        <label for="StartHourTime" class="col-sm-3">debut de l'heure :</label>
        <select name="StartHourTime">
            <?php
            for ($i = 0; $i < 24; $i++) {
                if ($i < 10) {
                    $H = "0" . $i;
                } else {
                    $H = $i;
                }
                for ($j = 0; $j < 2; $j++) {
                    if ($j % 2 == 0) {
                        $m = "00";
                    } else {
                        $m = "30";
                    }
                    $rslt = $H . ":" . $m
                    ?>
                    <option value="<?= $rslt; ?>"><?= $rslt ?></option>
                    <?php
                }
            }
            ?>
        </select>
    </div>
    <div class="row" style="margin:10px;">
        <label for="EndHourTime" class="col-sm-3">fin de l'heure :</label>
        <select name="EndHourTime">
            <?php
            for ($i = 0; $i < 24; $i++) {
                if ($i < 10) {
                    $H = "0" . $i;
                } else {
                    $H = $i;
                }
                for ($j = 0; $j < 2; $j++) {
                    if ($j % 2 == 0) {
                        $m = "00";
                    } else {
                        $m = "30";
                    }
                    $rslt = $H . ":" . $m
                    ?>
                    <option value="<?= $rslt; ?>"><?= $rslt ?></option>
                    <?php
                }
            }
            ?>
        </select>
    </div>
    <div class="row" style="margin:10px;">
        <label for="Location" class="col-sm-3">localisation</label>
        <input type="text" class="col-sm-1 form-control" name="Location" value="" autocomplete="off"
               required>
    </div>
    <div class="row col-sm-3" style="margin:10px;">
        <button><a href="../porte.php">Retour</a></button>
        <input type="submit" value="Submit" class="btn btn-primary">
    </div>
</form>

第 2 页:

<?php

session_start();

require "../vendor/autoload.php";

use myPHPnotes\Microsoft\Auth;
use myPHPnotes\Microsoft\Handlers\Session;
use myPHPnotes\Microsoft\Models\User;

$token=$_SESSION['token'];
$access_token= $token->access_token;

$StartDateTime = $_POST["StartDateTime"];
$StartHourTime =$_POST["StartHourTime"];
$EndHourTime =$_POST["EndHourTime"];
$Subject=$_POST["Subject"];
$Content=$_POST["Content"];
$Location=$_POST["Location"];
$localDatetime = "Pacific Standard Time";

$user = new User;
$addressmail = $user->data->getUserPrincipalName();
$prenom=$user->data->getGivenName();
$ID=$user->data->getId();


$_SESSION["tenant"]= $tenant="common";
$_SESSION["client_id"]= $client_id="{ME}";
$_SESSION["client_secret"]= $client_secret="{ME}";
$_SESSION["callback"]= $callback="http://localhost:8080/callback.php";
$_SESSION["scopes"]= $scopes=["User.Read"];
$_SESSION["ID"]=$ID;


$data = array(
    "subject" => $Subject,
    "body" => array(
        "contentType" => "HTML",
        "content" => $Content
    ),
    "start" => array(
        "dateTime" => $StartDateTime."T".$StartHourTime.":00",
        "timeZone" => "$localDatetime"
    ),
    "end" => array(
        "dateTime" => $StartDateTime."T".$EndHourTime.":00",
        "timeZone" => "$localDatetime"
    ),
    "location" => array(
        "displayName" => $Location
    ),
    "attendees"=> [array(
        "emailAddress"=> array(
            "address"=> $addressmail,
            "name"=>$prenom
        ),
        "type"=> "required"
    )]
);

$user = new User;
$ID=$user->data->getId();

$microsoft = new Auth($tenant , $client_id, $client_secret,$callback, $scopes);
$microsoft->Postcalendrier($ID,$data);
?>

功能后历:

public function Postcalendrier($ID,$data)
{

    $token=$_SESSION['token'];
    $calendarGroup="/calendarGroups/{ME}";
    $calendar="/calendars/{ME}/events";
    $urlcalendar="https://graph.microsoft.com/v1.0/users/".$ID;
    $url=$urlcalendar.$calendarGroup.$calendar;

    $headers = [
        'Authorization' => 'Bearer '.$token->access_token,
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
    ];

    var_dump(json_encode($data));
    var_dump($url);

    $reponse = $this->guzzle->request('POST', $url,[
        'headers' => $headers,
        'body' => json_encode($data),
        ],
        ['debug' => true]);

    return $reponse->getBody();
}

最后在 azure 活动目录上进行自动化:

希望我已经为您提供了尽可能多的信息,希望您能帮助我,感谢您的关注。

【问题讨论】:

    标签: php azure-active-directory guzzle azure-ad-graph-api


    【解决方案1】:

    {"error":{"code":"ErrorAccessDenied","message":"访问被拒绝。 检查凭据并重试。"}}

    要解决上述问题,当您在代码中使用 me 作为终点而不是需要使用 users/{USERID} 时,因为您有客户端凭据流 em> .

    例如:-

    $_SESSION["tenant"]= $tenant="common";
    $_SESSION["client_id"]= $client_id="{USER}";
    $_SESSION["client_secret"]= $client_secret="{USERID}";
    $_SESSION["callback"]= $callback="http://localhost:8080/callback.php";
    $_SESSION["scopes"]= $scopes=["User.Read"];
    $_SESSION["ID"]=$ID;
    

    你已经在你的function Postcalendrier - $urlcalendar="https://graph.microsoft.com/v1.0/users/".$ID; 中使用而不是那个使用 https://graph.microsoft.com/v1.0/users/usersID/calendar/getSchedule

    例如:-

    public function Postcalendrier($ID,$data)
    {
    
        $token=$_SESSION['token'];
        $calendarGroup="/calendarGroups/{ME}";
        $calendar="/calendars/{ME}/events";
      $urlcalendar="https://graph.microsoft.com/v1.0/users/usersID/calendar/getSchedule; //use this url to get the result
        $url=$urlcalendar.$calendarGroup.$calendar;
    
        $headers = [
            'Authorization' => 'Bearer '.$token->access_token,
            'Accept' => 'application/json',
            'Content-Type' => 'application/json',
        ];
    
        var_dump(json_encode($data));
        var_dump($url);
    
        $reponse = $this->guzzle->request('POST', $url,[
            'headers' => $headers,
            'body' => json_encode($data),
            ],
            ['debug' => true]);
    
        return $reponse->getBody();
    } 
    

    如需了解更多信息,请参考@vipulsparsh-MSFT建议的MS Q&A THREAD

    还有这个微服务社区讨论:Graph API - get calendar and events

    【讨论】:

      猜你喜欢
      • 2020-01-17
      • 1970-01-01
      • 2018-11-26
      • 2020-09-21
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多