避免使用 Microsoft Graph API 进行身份验证

时间:2021-01-25 14:53:38

标签: php microsoft-graph-api

我正在开发一个基本的 PHP 应用程序,以与 Microsoft Graph API 集成并允许用户在多人之间创建团队会议。

当我连接到我的 Office 365 帐户时,一切都在本地完美运行,但一旦注销,就会出现一个身份验证弹出窗口。

Microsoft Graph API 是否有避免这种手动身份验证的方法?

请在下面找到代码,一旦登录到我的 Office 365 帐户,我就能达到所需的结果。

function generateUrl() {
    $client_id = "clientId";
    $client_secret = "clientSecret";
    $redirect = http://localhost/add_teams_meeting;
    $scope = 'https%3A%2F%2Fgraph.microsoft.com%2FUser.read';

    $codeUrl = "https://login.microsoftonline.com/id/oauth2/v2.0/authorize?"
        . "client_id=$client_id"
        . "&scope=$scope"
        . "&response_type=code"
        . "&redirect_uri=$redirect";

    $this->redirect($codeUrl);
}

public function add_teams_meeting() {
    $code = $this->params->query['code'];

    $client_id = "clientId";
    $client_secret = "clientSecret";
    $redirect = http://localhost/add_teams_meeting;

    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://login.microsoftonline.com/id/oauth2/v2.0/token",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_SSL_VERIFYPEER => 0,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_HTTPHEADER => array(
        "Content-type" => "application/x-www-form-urlencoded",
        "Content-Length" => 144
      ),
      CURLOPT_POSTFIELDS => array(
        "grant_type" => "authorization_code",
        "client_id" => $client_id,
        "client_secret" => $client_secret,
        "code" => $code,
        'response_type' => 'code',
        'response_mode' => 'query',
        'state' => '12345',
        "redirect_uri" => $redirect),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      $result = json_decode($response);

      print_r($result);

      $access_token = $result->access_token; 
      $start = date('Y-m-d') . 'T' . '10:00';
      $end = date('Y-m-d') . 'T' . '10:45';
      

      $postData = array(
        'subject' => 'Teams meeting Sébastien Courtès/John Doe',
        'start' => array('dateTime' => $start, 'timeZone' => 'Europe/Paris'),
        'end' => array('dateTime' => $end, 'timeZone' => 'Europe/Paris'),
        'isOnlineMeeting' => 'true',
        'body' => array('content' => "", 'contentType' => 'html'),
        'attendees' => array(
          array(
            'emailAddress' => array(
              'address' => 'scourtes@domain.fr',
              'name' => 'Sébastien Courtès'
            ),
            'type' => 'Required'
          ),
          array(
            'emailAddress' => array(
              'address' => 'john.doe@gmail.com',
              'name' => 'John Doe'
            ),
            'type' => 'Required'
          )
        )
      );

      $postData = json_encode($postData);

      curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://graph.microsoft.com/v1.0/users/visio-myjobdating@jobinlive.net/events',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_HTTPHEADER => array(
          "Authorization: Bearer " . $access_token,
          "Content-Type: application/json"
        ),
        CURLOPT_POSTFIELDS => $postData
      ));

      $response = curl_exec($curl);
      $data = json_decode($response, true);

      print_r($data);
    }
    curl_close($curl);
}

//Result of $result;

stdClass Object
(
    [token_type] => Bearer
    [scope] => profile openid email https://graph.microsoft.com/Calendars.ReadWrite https://graph.microsoft.com/Calendars.ReadWrite.Shared https://graph.microsoft.com/Mail.Read https://graph.microsoft.com/OnlineMeetings.ReadWrite https://graph.microsoft.com/User.Read https://graph.microsoft.com/User.Read.All
    [expires_in] => 3599
    [ext_expires_in] => 3599
    [access_token] => eyJ0eXAiOiJKV1QiLCJub25jZSI6IjhYeW5kYUM4TlhlNHVzaWw5QWs1a [….]
)

// Result of $data
Array
(
    [@odata.context] => https://graph.microsoft.com/v1.0/$metadata#users('application%40domain.net')/events/$entity
    [@odata.etag] => W/"NnBqw/cCIkqFFyp0Mc5nRQAAJ2F3lQ=="
    [id] => AAMkADA2NmI2MzYxLTY5ZWYtNGJmNS05MWU1LWU5NDY5NTU5MTdjOQBGAAAAAADajXebCjTRR6OW2iVn9IBZBwA2cGrD9wIiSoUXKnQxzmdFAAAAAAENAAA2cGrD9wIiSoUXKnQxzmdFAAAnbPQBAAA=
    [createdDateTime] => 2021-01-25T16:20:45.4932869Z
    [lastModifiedDateTime] => 2021-01-25T16:20:48.6671925Z
    [changeKey] => NnBqw/cCIkqFFyp0Mc5nRQAAJ2F3lQ==
    [categories] => Array
        (
        )

    [transactionId] => 
    [originalStartTimeZone] => Europe/Paris
    [originalEndTimeZone] => Europe/Paris
    [iCalUId] => 040000008200E00074C5B7101A82E00800000000417FC50836F3D60100000000000000001000000024CF0715E241F14CB113453A4720278C
    [reminderMinutesBeforeStart] => 15
    [isReminderOn] => 1
    [hasAttachments] => 
    [subject] => Teams meeting Sébastien Courtès/John Doe
    [bodyPreview] => ________________________________________________________________________________
Microsoft Teams meeting
Join on your computer or mobile app
Click here to join the meeting
Learn More | Meeting options
_______________________________________________
    [importance] => normal
    [sensitivity] => normal
    [isAllDay] => 
    [isCancelled] => 
    [isOrganizer] => 1
    [responseRequested] => 1
    [seriesMasterId] => 
    [showAs] => busy
    [type] => singleInstance
    [webLink] => url
    [onlineMeetingUrl] => 
    [isOnlineMeeting] => 1
    [onlineMeetingProvider] => teamsForBusiness
    [allowNewTimeProposals] => 1
    [isDraft] => 
    [hideAttendees] => 
    [responseStatus] => Array
        (
            [response] => organizer
            [time] => 0001-01-01T00:00:00Z
        )

    [body] => Array
        (
            [contentType] => html
            [content] => 
________________________________________________________________________________

Microsoft Teams meeting

Join on your computer or mobile app

Click
 here to join the meeting 

Learn
 More | 
Meeting options 
________________________________________________________________________________

        )

    [start] => Array
        (
            [dateTime] => 2021-01-25T17:15:00.0000000
            [timeZone] => Europe/Paris
        )

    [end] => Array
        (
            [dateTime] => 2021-01-25T17:30:00.0000000
            [timeZone] => Europe/Paris
        )

    [location] => Array
        (
            [displayName] => 
            [locationType] => default
            [uniqueIdType] => unknown
            [address] => Array
                (
                )

            [coordinates] => Array
                (
                )

        )

    [locations] => Array
        (
        )

    [recurrence] => 
    [attendees] => Array
        (
            [0] => Array
                (
                    [type] => required
                    [status] => Array
                        (
                            [response] => none
                            [time] => 0001-01-01T00:00:00Z
                        )

                    [emailAddress] => Array
                        (
                            [name] => Sébastien Courtès
                            [address] => scourtes@domain.fr
                        )

                )

            [1] => Array
                (
                    [type] => required
                    [status] => Array
                        (
                            [response] => none
                            [time] => 0001-01-01T00:00:00Z
                        )

                    [emailAddress] => Array
                        (
                            [name] => John Doe
                            [address] => john.doe@gmail.com
                        )

                )

        )

    [organizer] => Array
        (
            [emailAddress] => Array
                (
                    [name] => Application Name
                    [address] => application@domain.net
                )

        )

    [onlineMeeting] => Array
        (
            [joinUrl] => joinUrl
        )

)

1 个答案:

答案 0 :(得分:0)

一般来说,您至少在某些时候无法绕过身份验证。解决方案将取决于您存储用户身份验证令牌的方式。由于您需要有权访问团队的任何会议帐户,这意味着注销的用户本质上是一个随机人,不应有权在团队内创建会议。

您可以阅读 Microsoft Graph here 的授权概述文档,但是您如何将这些用户令牌存储在用户的计算机上将取决于您实施的存储类型。

相关问题