microsoft graph api 以编程方式创建在线会议但面临 403 错误

microsoft graph api to create online meeting programmatically but facing 403 error

我正在使用 Microsoft Graph Api (PHP->msGraph SDK) 创建在线会议。 我遇到 403 错误有人可以帮助我。

$clientId = "***********************************";
$clientSecret = "***********************************";
$tenantId = '***********************************';
$responseUri = "http://localhost:8888/moodle39";



$guzzle = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
    'form_params' => [
        'client_id' => $clientId,
        'client_secret' => $clientSecret,
        'scope' => 'https://graph.microsoft.com/.default',
        'grant_type' => 'client_credentials',
    ],
])->getBody()->getContents());
$accessToken = $token->access_token;

//Create a new Graph client. 
$graph = new Graph(); 
$graph->setAccessToken($accessToken);

$onlinemeet->startDateTime = "2020-09-02T14:30:34.2444915";
$onlinemeet->endDateTime = "2020-09-02T15:30:34.2444915";
$onlinemeet->subject = "Test Meeting";
$jso = json_encode($onlinemeet);
$user = $graph->createRequest("POST", "/me/onlineMeetings")->addHeaders(array("Content-Type" => "application/json"))->attachBody($jso)->setReturnType(User::class) ->execute();

异常 - 客户端错误:POST https://graph.microsoft.com/beta/me/onlineMeetings 导致 403 禁止响应:{“错误”:{“代码”:“禁止”,“消息”: "", "innerError": { "request-id": "bd43aa57-511e-4 (截断...)

在 Azure 门户中创建应用程序时

在 API 权限下我授予了访问权限

GraphApi->Delegated Permissions->onlinemeetings.ReadWrite.

有人可以在 PHP.

中用正确的示例或正确的语法帮助我吗

谢谢!!..

您不能使用 client credential flow to get the token to call the /me endpoint. For the client credential flow, it is usually used for server-to-server interactions that must run in the background and do not interact with the user immediately(No user logged in). For the /me endpoint, it is usually User login is required, so you should use auth code flow

顺便说一句,Microsoft Graph 中 /beta 版本下的 API 可能会发生变化。不支持在生产应用程序中使用这些 API。所以建议大家使用/v1.0版本

请看:here.


更新:

类似的例子还有很多,希望对你有所帮助:

OAuth 2.0 PHP Sample Code.

Authentication and Authorization Using Auth0 in PHP.