PHP 中的 Office 365 api

Office 365 api in PHP

我正在尝试为我的公司创建日历视图以显示哪些员工有空。我在 Azure 中做了一个应用程序,我可以登录系统,我获得了访问令牌,但我无法获取信息。

我使用 curl 构建请求 url 并将此 header 与我的请求

一起发送
array(6) {
 [0]=>
  string(28) "User-Agent: php-tutorial/1.0"
  [1]=>
  string(588) "Authorization: Bearer ~ACCESS_TOKEN~"
  [2]=>
  string(24) "Accept: application/json"
  [3]=>
  string(55) "client-request-id: ~GUID~"
  [4]=>
  string(30) "return-client-request-id: true"
  [5]=>
  string(45) "X-AnchorMailbox: ~MAIL~"
}

我得到的回复是:“”

我做错了什么?

更新

登录后,Microsoft 不会询问我是否要授予对该应用程序的访问权限,但会授予我访问令牌。这可能是问题所在吗?我怎样才能让它请求许可?

更新 2

登录并尝试获取信息后,我收到 401 错误消息:访问被拒绝。我认为这很奇怪,因为它已经授予我访问令牌。

更新 3

又过了几天,我尝试了一些示例和教程代码来检查它是否有效。但即使在这些应用程序中,api 似乎也不起作用。 这些是我尝试过的项目。

https://github.com/microsoftgraph/php-connect-rest-sample

https://dev.outlook.com/restapi/tutorial/php

根据 https://graph.microsoft.io/en-us/docs/overview/errors 处的错误列表,当您收到 401 错误时,表示

Required authentication information is either missing or not valid for the resource.

可能是您的访问令牌不包含您的应用程序请求的资源的范围。例如

如果要列出日历视图的资源,根据https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/user_list_calendarview的文档:

One of the following scopes is required to execute this API: Calendars.Read; Calendars.ReadWrite

请登录您的 Azure AD 应用程序门户,检查 Microsoft Graph 的权限 Have full access to user calendarsRead user calendars 是否已标记。

您可以使用访问令牌检查响应正文的 "scope" 部分,请参阅 https://azure.microsoft.com/en-us/documentation/articles/active-directory-protocols-oauth-code/#use-the-authorization-code-to-request-an-access-token 了解更多信息。

问题很旧,但我会 post 对我有用的解决方案。

与您一样,我努力设置 php 客户端以使用 Office365 (Sharepoint)。官方文档缺少示例(卷曲/请求/响应等),因此很难设置自定义客户端。

最后我找到了这个库:https://github.com/vgrem/phpSPO - 有了它我设法通过 PHP 进行身份验证并在 Sharepoint 中做我需要的东西。