Microsoft Graph API不断返回未经授权的401

时间:2019-04-01 13:44:40

标签: microsoft-graph

请不要重复报告,因为所有其他主题都无法解决我的问题,谢谢。

我正在使用Microsoft Graph API列出和创建日历事件,但是,我似乎无法使findMeetingTimes端点正常工作。

这是我的授权URL:

https://login.microsoftonline.com/consumers/oauth2/v2.0/authorize
  ?client_id=xxxxx
  &response_type=code
  &redirect_uri=xxxxx
  &response_mode=form_post
  &scope=openid profile User.ReadWrite User.ReadBasic.All Sites.ReadWrite.All Contacts.ReadWrite People.Read Notes.ReadWrite.All Tasks.ReadWrite Mail.ReadWrite Files.ReadWrite.All Calendars.ReadWrite
  &state=12345

然后,我使用相同的范围请求访问令牌:

$request = json_decode($guzzle->post('https://login.microsoftonline.com/common/oauth2/v2.0/token', [
    'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
    'form_params' => [
        'client_id' => 'xxxxx',
        'scope' => 'openid profile User.ReadWrite User.ReadBasic.All Sites.ReadWrite.All Contacts.ReadWrite People.Read Notes.ReadWrite.All Tasks.ReadWrite Mail.ReadWrite Files.ReadWrite.All Calendars.ReadWrite',
        'code' => $auth_code,
        'grant_type' => 'authorization_code',
        'redirect_uri' => 'xxxxx',
        'client_secret' => 'xxxxx'
    ],
])->getBody()->getContents());

之后,我使用以下方法尝试findMeetingTimes端点:

$request = json_decode($guzzle->post('https://graph.microsoft.com/v1.0/me/findMeetingTimes', [
    'headers' => [
        'Authorization' => 'Bearer ' . $accessToken,
        'Content-Type' => 'application/json',
    ],
    'body' => '{
        "attendees": [
        {
            "type": "required",
            "emailAddress": {
            "name": "My Name",
            "address": "myemail@hotmail.com"
            }
        }
        ],
        "timeConstraint": {
        "activityDomain":"work",
        "timeslots": [
            {
            "start": {
                "dateTime": "2019-04-02T09:00:00",
                "timeZone": "Pacific Standard Time"
            },
            "end": {
                "dateTime": "2019-04-03T17:00:00",
                "timeZone": "Pacific Standard Time"
            }
            }
        ]
        }
    }',
    ])->getBody()->getContents());

但是,它不断返回:

  

401-未经授权:由于凭据无效,访问被拒绝。

     

您无权使用您提供的凭据查看此目录或页面。

有人知道我想念什么吗?请注意,我使用的不是普通的工作/学校帐户,而是Microsoft的工作/学校帐户。 (有关系吗?)

1 个答案:

答案 0 :(得分:2)

无法使用个人帐户来呼叫此端点,这在documentation

中非常清楚地提及

enter image description here